Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Internet Applications, Lecture 4 What is JSP ? Elements of a JSP document JavaBeans and JSP TagLibs, JSTL JSP and Java Servlets JSP v2.0 Java Server Pages™ A technology for serving dynamic web content Summary Stanisław Osiński, [email protected] JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Agenda What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary • • • • • • • • What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling Custom Tags, JSP Standard Tag Library JSP and Java Servlets JSP v2.0 – new features Summary JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 What is JSP ? What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets „The JavaServer Pages technology provides the means for textual specification of the creation of a dynamic response to a request” (JSP Spec. v1.2) JSP v2.0 Summary JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 What is JSP ? What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP is a textual specification of the creation of a dynamic response to a request (JSP Spec. v1.2) /** */ public class SomeServlet extends HttpServlet { /** */ protected void doGet(...) { // ... out.println(„<HTML><TITLE>Title</TITLE>”); out.println(„<BODY><P>Today is:”); JSP v2.0 out.println(new java.util.Date().toString()); Summary out.println(„</P></BODY>”); out.println(„</HTML>”); out.close(); } } JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 What is JSP ? What is JSP ? Elements of a JSP document JSP is a textual specification of the creation of a dynamic response to a request (JSP Spec. v1.2) JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets <HTML> <TITLE>Title</TITLE>; <BODY> <P> Today is: <%= new java.util.Date().toString() %> JSP v2.0 Summary </P> </BODY> </HTML> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Elements of a JSP document Comment not copied to the output document What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary <%-- An example JSP page --%> Directives page – JSP document properties <%@page import=„java.util.*" contentType="text/html" session="true" %> Static content written verbatim to the output document <HTML> <TITLE>Title</TITLE> <BODY> <P> Today is: Declarations of global variables and methods Java code ("scriptlets") executed on request time Expressions evaluated on request time Actions executed on request time <%! String date; %> <% date = new Date().toString(); %> <%= date %> </P> <jsp:include page="logo.jsp"/> </BODY> </HTML> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Implicit Objects What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary In every JSP document a number of implicit objects can be accessed: • • • • • • • application (javax.servlet.ServletContext) config (javax.servlet.ServletConfig) out (javax.servlet.jsp.JspWriter) request (javax.servlet.ServletRequest) response (java.servlet.ServletResponse) session (javax.servlet.http.HttpSession) exception (java.lang.Throwable) JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JavaBeans in JSP (1) package org.stachoodev.examples.*; What is JSP ? Elements of a JSP document JavaBeans and JSP /** */ public class Login { /** */ private String userId, password; /** */ public void setUserId(String userId) { this.userId = userId; } Exception handling TagLibs, JSTL JSP and Java Servlets /** */ public void setPassword(String password) { this.password = password; } JSP v2.0 Summary /** */ public boolean isLoginCorrect() { return userId.equals(password); } } JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JavaBeans in JSP (2) What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary <%@page import="org.stachoodev.examples.*" contentType="text/html" %> <HTML> <TITLE>Login</TITLE> <BODY> <P> <% Login login = new Login(); login.setUserId(request.getParameter("userId”)); login.setPassword(request.getParameter("password”)); %> <% if (login.isLoginCorrect()) { %> Login successful. <% } else { %> Login failed. <% } %> </P> </BODY> </HTML> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JavaBeans in JSP (3) What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary <%@page import="org.stachoodev.examples.*" contentType="text/html" %> <jsp:useBean id="login" scope="page" class="Login"/> <HTML> <TITLE>Login</TITLE> <BODY> <P> <% login.setUserId(request.getParameter("userId")); login.setPassword(request.getParameter("password")); %> <% if (login.isLoginCorrect()) { %> Login successful. <% } else { %> Login failed. <% } %> </P> </BODY> </HTML> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JavaBean instance scope What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary The scope parameter of the jsp:useBean action determines the "visibility" of a JavaBean instance: • page – the bean is visible only within the JSP page containing its declaration • request – the bean is visible in any JSP page processing the same request • session – the instance is accessible across the current user session • application – any JSP page can access the JavaBean instance JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JavaBeans in JSP (4) What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary <%@page import="org.stachoodev.examples.*" contentType="text/html" %> <jsp:useBean id="login" scope="page" class="Login" /> <jsp:setProperty name="login" property="userId" value="<%= request.getParameter("userId") %>" /> <jsp:setProperty name="login" property="passowrd" value="<%= request.getParameter("password") %>" /> <HTML> <TITLE>Login</TITLE> <BODY> <P> <% if (login.isLoginCorrect()) { %> Login successful. <% } else { %> Login failed. <% } %> </P> </BODY> </HTML> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JavaBeans in JSP (5) What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary <%@page import="org.stachoodev.examples.*" contentType="text/html" %> <jsp:useBean id="login" scope="page" class="Login" /> <jsp:setProperty name="login" property="*" /> <HTML> <TITLE>Login</TITLE> <BODY> <P> <% if (login.isLoginCorrect()) { %> Login successful. <% } else { %> Login failed. <% } %> </P> </BODY> </HTML> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Exception handling <%@page contentType="text/html" errorPage="error.jsp" %> What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary <HTML> <TITLE>Test page</TITLE> <BODY> <P> <% ((String)null).length(); %> <%@page contentType="text/html" </P> isErrorPage="true" %> </BODY> </HTML> <HTML> <TITLE>Error</TITLE> <BODY> <P> An error has occurred: <%= exception.getMessage() %> </P> </BODY> </HTML> error.jsp JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Including and forwarding What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary <%@page import="org.stachoodev.examples.*" contentType="text/html" %> <jsp:useBean id="login" scope="page" class="Login" /> <jsp:setProperty name="login" property="*" /> <%@include file="page_header.html" %> <% if (login.isLoginCorrect()) { %> <jsp:forward page="welcome.jsp" /> <% } else { %> <jsp:include page="forgotten.jsp" /> <% } %> <%@include file="page_footer.html" %> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Custom Tags (1) What is JSP ? Elements of a JSP document JavaBeans and JSP <%@page import="org.stachoodev.examples.*" contentType="text/html" %> Exception handling <%@taglib prefix="util" uri="util.tld" %> TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary <%@include file="page_header.html" %> <util:login request="<%= request %>" /> <%@include file="page_footer.html" %> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Custom Tags (2) To implement a Custom JSP Tag: What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets • Create the tag handler class (doStartTag(), doEndTag(), ...) • Create the class describing the variables accesible within the Custom Tag (optional) • Create the Tag Library Descriptor (TLD file) JSP v2.0 Summary JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JSP Standard Tag Library A standardized library of JSP Custom Tags: What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary • • • • • • • • conditions, loops, enumerations URL transformations content localization formatting of numbers and dates SQL queries XML processing XSLT transformations validation of JSP documents JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JSP and Java Servlets What is JSP ? execution Elements of a JSP document JavaBeans and JSP Exception handling Java Servlet HTTP request TagLibs, JSTL JSP and Java Servlets User Agent (IE, Netscape, ...) JSP Container JSP v2.0 Summary generation + compilation <%@page import="java.util.*" %> <HTML> <TITLE>Title</TITLE>; <BODY> <P> Today is: <%= new Date().toString() %> </P> </BODY> </HTML> JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JSP and Java Servlets <%@page import=„java.util.*" %> What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets <HTML> package org.apache.jsp; <TITLE>JSP test</TITLE> <BODY> import java.util.*; <P> import javax.servlet.*; <%! String date; %> // ... <% date = public class test$jsp extends HttpJspBase { new Date().toString(); %> // begin [file="/test.jsp";from=(8,9);to=(8,23)] Today is: String date; <%= date %> // end </P> </BODY> public void _jspService(...) { </HTML> // ... session = pageContext.getSession(); // HTML [file="/test.jsp";from=(2,36);to=(8,6)] out.write("\r\n\r\n<HTML>\r\n<TITLE>JSP..."); // end JSP v2.0 Summary // begin [file="/test.jsp";from=(9,8);to=(11,6)] date = new Date().toString(); // end // begin [file="/test.jsp";from=(14,9);to=(14,15)] out.print( date ); // end } } JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 JSP Specification v2.0 A lot of changes and improvements: What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets • Expression Language (EL) – scriptless JSP • Tag Files • based on Servlet Specification v2.4 • requires Java 2 Platform v1.4 • to be implemented in Tomcat 5.0 JSP v2.0 Summary JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Summary What is JSP ? JSP vs. Java Servlets: Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary • better, though still imperfect, codecontent separation • Tag Libraries - extensibility and component reuse • faster development (less typing ;) • development tools available JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 Summary What is JSP ? Elements of a JSP document JavaServer Pages Home http://java.sun.com/jsp/ JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary JSP Standard Templates Library http://java.sun.com/products/jsp/jstl/ Tomcat – Reference Implementation http://jakarta.apache.org/tomcat JSP – A technology for serving dynamic web content Stanisław Osiński, 2002 What is JSP ? Elements of a JSP document JavaBeans and JSP Thank you for your attention Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary Java Server Pages A technology for serving dynamic web content Stanisław Osiński, [email protected] JSP – A technology for serving dynamic web content Stanisław Osiński, 2002