Objectives Objectives � How to program Web applications using JSP An Introduction to XML and Web Technologies An Introduction to XML and Web Technologies � How to extend the JSP syntax using tag libraries Programming Web Applications Programming Web Applications � The relation to Servlets with JSP with JSP Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley An Introduction to XML and Web Technologies 2 The JSP Framework A Tiny Example The JSP Framework A Tiny Example � Servlets make heavy use of Java and require <% response.ad <% response.addDateHeader("E dDateHeader("Expires", 0); % xpires", 0); %> <html> sophisticated programming <head><title>JSP</title></head> � JSP views a Web application as a collection of <body> <h1>Hello World!</h1> active pages <%! int hits = 0; %> <%! int hi ts = 0; %> � The pages are HTML with snippets of code You are visitor number <% synchro <% synchronized(this) { nized(this) { out.println(++ out.println(++hits); } %> hits); } %> � JSP pages are translated into servlets since the last time the service was restarted. <p> This page was last updated: <%= new java.util.Date() <%= new ja va.util.Date().toLocaleStrin .toLocaleString() %> g() %> </body> </html> An Introduction to XML and Web Technologies 3 An Introduction to XML and Web Technologies 4 1
JSP Templates JSP Expressions JSP Templates JSP Expressions � A text file with snippets of Java code: <html> • expressions <head><title>Addition</title></head> • statements <body> • declarations The sum of <%= request.g <%= request.getParameter("x etParameter("x") %> ") %> and <%= re <%= request.getParam quest.getParameter("y") %> eter("y") %> is � JSP directives <%= Intege <%= Integer.parseInt(req r.parseInt(request.getParame uest.getParameter("x")) + ter("x")) + � Implicitly declared variables: Integer.parseInt(req Intege r.parseInt(request.getParame uest.getParameter("y")) %> ter("y")) %> • HttpServletRequest req reque uest st; </body> • HttpServletResponse re resp spon onse se; </html> • HttpSession se sess ssio ion; • ServletContext app appli lica cati tion on; • ServletConfig co conf nfig ig; • JspWriter ou out; • PageContext pa page geCo Cont ntex ext; An Introduction to XML and Web Technologies 5 An Introduction to XML and Web Technologies 6 JSP Statements JSP Declarations JSP Statements JSP Declarations <html> <%! int add(String x, String <%! int add(St ring x, String y) { y) { <head><title>Numbers</title></head> return Integer.parseIn return I nteger.parseInt(x)+Integer.p t(x)+Integer.parseInt(y); arseInt(y); <body> } <ul> %> %> <% int n = <% int n = <html> Integer.parseIn nteger.parseInt(request.getP t(request.getParameter("n")) arameter("n")); <head><title>Addition</title></head> for ( for (int i=0; i<n; int i=0; i<n; i++) i++) <body> out out.println("<li> .println("<li>"+i+"</li>"); "+i+"</li>"); The sum of <%= request.getParameter("x") %> %> %> and <%= request.getParameter("y") %> is </ul> <%= add(request.getParameter("x"), </body> request.getParameter("y")) %> </html> </body> </html> An Introduction to XML and Web Technologies 7 An Introduction to XML and Web Technologies 8 2
Be Careful About Declarations (1/2) Be Careful About Declarations (2/2) Be Careful About Declarations (1/2) Be Careful About Declarations (2/2) <% response.addDateHeader("Expires", 0); %> <% response.addDateHeader("Expires", 0); %> <html> <html> <head><title>JSP</title></head> <head><title>JSP</title></head> <body> <body> <h1>Hello World!</h1> <h1>Hello World!</h1> <%! int <%! in t hit hits = s = 0 0; % %> <% int h <% int hits its = = 0; 0; %> %> You are visitor number You are visitor number <% synchronized(this) { out.println(++hits); } %> <% synchronized(this) { out.println(++hits); } %> since the last time the service was restarted. since the last time the service was restarted. <p> <p> This page was last updated: This page was last updated: <%= new java.util.Date().toLocaleString() %> <%= new java.util.Date().toLocaleString() %> </body> </body> </html> </html> This page counter is always 1... An Introduction to XML and Web Technologies 9 An Introduction to XML and Web Technologies 10 JSP Directives Other Directives JSP Directives Other Directives header.jsp � contentType=" type " <html> <head><title><%= title %></title></head> � info=” description " <body> footer.jsp � errorPage=" path " </body> � isErrorPage=" boolean " </html> � import=" package " <%! String title = "Addition"; %> <%@ incl <%@ nclud ude fil e file=" e="he heade ader. r.js jsp" %> p" %> The sum of <%= request.getParameter("x") %> and <%= request.getParameter("y") %> is <%= Integer.parseInt(request.getParameter("x")) + Integer.parseInt(request.getParameter("y")) %> <%@ <%@ incl nclud ude fil e file=" e="fo foote oter. r.js jsp" %> p" %> An Introduction to XML and Web Technologies 11 An Introduction to XML and Web Technologies 12 3
Using Error Pages Translation into Servlets Using Error Pages Translation into Servlets <%@ <%@ p page age e errorP orPage age=" ="err error or.j .jsp" sp" %> %> <html> <% <% re respo spons nse. e.add addDat DateH eHead eader er(" ("Exp Expire ires" s", 0 , 0); % %> <head><title>Division</title></head> <html> <body> <head><title>JSP</title></head> <body> <% int n = Integer.parseInt(request.getParameter("n")); %> <% int m = Integer.parseInt(request.getParameter("m")); %> <h1>Hello World!</h1> <%= n %>/<%= m %> equals <%= n/m %> <%! in <%! int t hit hits = s = 0 0; % %> You are visitor number </body> </html> <% syn <% synch chron ronize ized( d(thi this) s) { { ou out.p t.pri rintl ntln( n(++ ++hit hits); s); } } %> %> since the last time the service was restarted. <p> <%@ p <%@ page age i isErro rrorPa rPage ge="t ="tru rue" e" %> %> <html> This page was last updated: <head><title>Error</title></head> <%= ne <%= new w jav java.u a.uti til.D l.Dat ate( e().t ).toLo oLoca caleS leStr trin ing() g() %> %> <body> </body> Something bad happened: </html> <%= except exception ion.getMessage() %> </body> </html> An Introduction to XML and Web Technologies 13 An Introduction to XML and Web Technologies 14 The Generated Servlet (1/5) The Generated Servlet (2/5) The Generated Servlet (1/5) The Generated Servlet (2/5) package org.apache.jsp; public void _jspService(HttpServletRequest request, HttpServletResponse response) import javax.servlet.*; throws java.io.IOException, ServletException { import javax.servlet.http.*; import javax.servlet.jsp.*; JspFactory _jspxFactory = null; PageContext pageContext = null; public final class hello_jsp HttpSession session = null; extends org.apache.jasper.runtime.HttpJspBase ServletContext application = null; implements org.apache.jasper.runtime.JspSourceDependent { ServletConfig config = null; JspWriter out = null; int hi int hits ts = = 0; 0; Object page = this; private static java.util.Vector _jspx_dependants; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; public java.util.List getDependants() { return _jspx_dependants; } An Introduction to XML and Web Technologies 15 An Introduction to XML and Web Technologies 16 4
Recommend
More recommend