1 / 15

HTML Template Languages: Separating Server-side Logic from HTML Presentation

HTML template languages, such as JSP, ASP, JavaScript, VBScript, and PHP, are designed to separate server-side logic from HTML presentation. They allow for variable substitution, page composition, and executing server-side code. This lecture covers the features and implementation of HTML template languages.

clessman
Télécharger la présentation

HTML Template Languages: Separating Server-side Logic from HTML Presentation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 190: Internet E-Commerce Lecture 7

  2. 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

  3. 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

  4. 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 Template systems: CGI differences

  5. JSP: Start with HTML(Source for hello.jsp) <html> <head><title>Hello World</title></head> <body bgcolor=#FFFFFF> <h2>Hello World</h2> </body> </html>

  6. JSP: Variable substitution <html> <% String titleString = “Hello World”; %> <head><title> <%= titleString %></title></head> <body> <h2><%= titleString %></h2> </body> </html>

  7. JSP: Page Composition <jsp:include page=“header.html” /> <body bgcolor=#FFFFFF> <h2>Hello World</h2> <jsp:include page=“footer.html” />

  8. JSP: Page composition • <jsp:include>: Also evaluates any JSP tags within the included document (allows nesting) • To avoid nested evaluation, use <%@ include file=“filename.html” %>

  9. JSP: Server side code <html> <head> </head> <body> <%-- Generate two Prime Tables --%> The primes to 10:<br> <%= getPrimes(10) %> <br> The primes to 20:<br> <%= getPrimes(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("<table>"); sb.append(" <tr>"); sb.append(" <th>"); sb.append(" number"); sb.append(" </th>"); sb.append(" <th>"); sb.append(" prime"); sb.append(" </th>"); sb.append(" </tr>");

  10. 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(" <tr>"); sb.append(" <td>"); sb.append(" "+i); sb.append(" </td>"); sb.append(" <td>"); if (found) { sb.append(" no"); } else { sb.append(" yes"); } sb.append(" </td>"); sb.append(" </tr>"); } // End of the table sb.append("</table>"); return sb.toString(); } %> </body> </html>

  11. Three major scopes Request Session Application Detail: JSP supports “page” scope What happens to “session” variable if JSP server stops running? JSP: Object scope

  12. 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 • RequestCookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2 ... • ResponseSet-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure • Specification: http://www.netscape.com/newsref/std/cookie_spec.html

  13. 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("<HTML>"); out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>"); out.println("<BODY>"); out.println("<BIG>Hello World</BIG>"); out.println("</BODY></HTML>"); } }

  14. ASP (Active Server Pages) Examples • Variable substitution…<%= variableName %>…. • Page composition<!--#include file=“footer.asp”--> • Server side code<%@ language="javascript" %><html> <head><% function jsproc(num1,num2) { Response.Write(num1*num2) } %> </head><body>The result of the calculation is: <%jsproc(3,4)%> </body></html> • Reference: http://www.w3schools.com/asp/asp_intro.asp

  15. PHP Examples • Variable substitution:<?php echo $HTTP_USER_AGENT; ?> • Page composition<?php require( "header.inc"); ?><?php include( "header.inc"); ?> • Server side code<?phpif( strstr( $HTTP_USER_AGENT, "MSIE")) {    echo "You are using Internet Explorer<br>";}?> • Reference: http://www.php.net/manual/en/

More Related