Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński, What is JSP ? Elements of a JSP document JavaBeans and JSP TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary Internet Applications, Lecture 4
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Agenda 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 What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content What is JSP ? „The JavaServer Pages technology provides the means for textual specification of the creation of a dynamic response to a request” (JSP Spec. v1.2) What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content What is JSP ? 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(„ Title ”); out.println(„ Today is:”); out.println(new java.util.Date().toString()); out.println(„ ”); out.close(); } /** */ public class SomeServlet extends HttpServlet { /** */ protected void doGet(...) { //... out.println(„ Title ”); out.println(„ Today is:”); out.println(new java.util.Date().toString()); out.println(„ ”); out.close(); } What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content What is JSP ? Title ; Today is: Title ; Today is: JSP is a textual specification of the creation of a dynamic response to a request (JSP Spec. v1.2) What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Elements of a JSP document import=„java.util.*" contentType="text/html" session="true" %> Title Today is: <% date = new Date().toString(); %> import=„java.util.*" contentType="text/html" session="true" %> Title Today is: <% date = new Date().toString(); %> Directives page – JSP document properties Static content written verbatim to the output document Declarations of global variables and methods Java code ("scriptlets") executed on request time Expressions evaluated on request time Actions executed on request time 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
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Implicit Objects 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) What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JavaBeans in JSP (1) package org.stachoodev.examples.*; /** */ public class Login { /** */ private String userId, password; /** */ public void setUserId(String userId) { this.userId = userId; } /** */ public void setPassword(String password) { this.password = password; } /** */ public boolean isLoginCorrect() { return userId.equals(password); } package org.stachoodev.examples.*; /** */ public class Login { /** */ private String userId, password; /** */ public void setUserId(String userId) { this.userId = userId; } /** */ public void setPassword(String password) { this.password = password; } /** */ public boolean isLoginCorrect() { return userId.equals(password); } What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JavaBeans in JSP (2) import="org.stachoodev.examples.*" contentType="text/html" %> Login <% Login login = new Login(); login.setUserId(request.getParameter("userId”)); login.setPassword(request.getParameter("password”)); %> Login successful. Login failed. import="org.stachoodev.examples.*" contentType="text/html" %> Login <% Login login = new Login(); login.setUserId(request.getParameter("userId”)); login.setPassword(request.getParameter("password”)); %> Login successful. Login failed. What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JavaBeans in JSP (3) import="org.stachoodev.examples.*" contentType="text/html" %> Login <% login.setUserId(request.getParameter("userId")); login.setPassword(request.getParameter("password")); %> Login successful. Login failed. import="org.stachoodev.examples.*" contentType="text/html" %> Login <% login.setUserId(request.getParameter("userId")); login.setPassword(request.getParameter("password")); %> Login successful. Login failed. What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JavaBean instance scope 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 What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JavaBeans in JSP (4) import="org.stachoodev.examples.*" contentType="text/html" %> <jsp:setProperty name="login" property="userId" value=" " /> <jsp:setProperty name="login" property="passowrd" value=" " /> Login Login successful. Login failed. import="org.stachoodev.examples.*" contentType="text/html" %> <jsp:setProperty name="login" property="userId" value=" " /> <jsp:setProperty name="login" property="passowrd" value=" " /> Login Login successful. Login failed. What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JavaBeans in JSP (5) import="org.stachoodev.examples.*" contentType="text/html" %> Login Login successful. Login failed. import="org.stachoodev.examples.*" contentType="text/html" %> Login Login successful. Login failed. What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Exception handling What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary contentType="text/html" errorPage="error.jsp" %> Test page <% ((String)null).length(); %> contentType="text/html" errorPage="error.jsp" %> Test page <% ((String)null).length(); %> contentType="text/html" isErrorPage="true" %> Error An error has occurred: contentType="text/html" isErrorPage="true" %> Error An error has occurred: error.jsp
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Including and forwarding import="org.stachoodev.examples.*" contentType="text/html" %> import="org.stachoodev.examples.*" contentType="text/html" %> What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Custom Tags (1) import="org.stachoodev.examples.*" contentType="text/html" %> prefix="util" uri="util.tld" %> " /> import="org.stachoodev.examples.*" contentType="text/html" %> prefix="util" uri="util.tld" %> " /> What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Custom Tags (2) To implement a Custom JSP Tag: 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) What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JSP Standard Tag Library A standardized library of JSP Custom Tags: conditions, loops, enumerations URL transformations content localization formatting of numbers and dates SQL queries XML processing XSLT transformations validation of JSP documents What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JSP and Java Servlets Title ; Today is: Title ; Today is: User Agent (IE, Netscape,...) JSP Container generation + compilation execution HTTP request Java Servlet What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JSP test <% date = new Date().toString(); %> Today is: JSP test <% date = new Date().toString(); %> Today is: JSP and Java Servlets package org.apache.jsp; import java.util.*; import javax.servlet.*; //... public class test$jsp extends HttpJspBase { // begin [file="/test.jsp";from=(8,9);to=(8,23)] String date; // end public void _jspService(...) { //... session = pageContext.getSession(); // HTML [file="/test.jsp";from=(2,36);to=(8,6)] out.write("\r\n\r\n \r\n JSP..."); // end // 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 } package org.apache.jsp; import java.util.*; import javax.servlet.*; //... public class test$jsp extends HttpJspBase { // begin [file="/test.jsp";from=(8,9);to=(8,23)] String date; // end public void _jspService(...) { //... session = pageContext.getSession(); // HTML [file="/test.jsp";from=(2,36);to=(8,6)] out.write("\r\n\r\n \r\n JSP..."); // end // 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 } What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content JSP Specification v2.0 A lot of changes and improvements: 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 What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Summary JSP vs. Java Servlets: better, though still imperfect, code- content separation Tag Libraries - extensibility and component reuse faster development (less typing ;) development tools available What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Summary JavaServer Pages Home JSP Standard Templates Library Tomcat – Reference Implementation What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages A technology for serving dynamic web content Stanisław Osiński, Thank you for your attention What is JSP ? Elements of a JSP document JavaBeans and JSP Exception handling TagLibs, JSTL JSP and Java Servlets JSP v2.0 Summary