CSE 190: Internet E-Commerce Lecture 7
HTML Templates Designed to separate server side logic from HTML presentation Key features –Escapes from HTML into template language –Page compositing –Variable substitution –Executing server side code
HTML Template languages JSP (Java Server Pages) ASP (Active Server Pages) –Javascript, VBScript Text::Template (Perl module) PHP (PHP Hypertext Processor) Roll your own implementation
Template systems: CGI differences CGI One process per request Request data given by environment variable or via stdin Most template systems Are processed within a thread of the server process –Faster performance Input passed by a reference to a standard “request” variable
JSP: Start with HTML (Source for hello.jsp) Hello World Hello World
JSP: Variable substitution <% String titleString = “Hello World”; %>
JSP: Page Composition Hello World
JSP: Page composition : Also evaluates any JSP tags within the included document (allows nesting) To avoid nested evaluation, use
JSP: Server side code The primes to 10: The primes to 20: <%-- Separate primes from non primes and generate a table This time as an expression tag defining a function. --%> <%! private String getPrimes(int max) { StringBuffer sb = new StringBuffer(); // Table headers sb.append(" "); sb.append(" number"); sb.append(" "); sb.append(" prime"); sb.append(" ");
JSP: Server side (cot’d) // Check for primes for(int i=2; i<=max; i++) { boolean found = false; for(int j=2; j<=Math.sqrt(i); j++) { if(i%j == 0) { found=true; break; } // Table body sb.append(" "); sb.append(" "+i); sb.append(" "); if (found) { sb.append(" no"); } else { sb.append(" yes"); } sb.append(" "); } // End of the table sb.append(" "); return sb.toString(); } %>
JSP: Object scope Three major scopes –Request –Session –Application Detail: JSP supports “page” scope What happens to “session” variable if JSP server stops running?
Session scope: Cookies Cookies: Allow HTTP server to recognize when the same browser makes a new follow up request Two types of cookies: session, persistent HTTP header syntax –Request Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2... –Response Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure Specification:
JSP: Implementation import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType( "text/html"); PrintWriter out = res.getWriter(); out.println(" "); out.println(" Hello World "); out.println(" "); out.println(" Hello World "); out.println(" "); }
ASP (Active Server Pages) Examples Variable substitution … …. Page composition Server side code The result of the calculation is: Reference:
PHP Examples Variable substitution: Page composition Server side code "; } ?> Reference: