stanisław osiński, 2002jsp – a technology for serving dynamic web content java server pages™ a...
Post on 13-Jan-2016
219 Views
Preview:
TRANSCRIPT
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
Java Server Pages™
A technology for serving dynamic web content
Stanisław Osiński, stachoo@man.poznan.pl
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
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• 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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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(„<HTML><TITLE>Title</TITLE>”); out.println(„<BODY><P>Today is:”);
out.println(new java.util.Date().toString());
out.println(„</P></BODY>”); out.println(„</HTML>”);
out.close(); }}
/** */public class SomeServlet extends HttpServlet { /** */ protected void doGet(...) { // ... out.println(„<HTML><TITLE>Title</TITLE>”); out.println(„<BODY><P>Today is:”);
out.println(new java.util.Date().toString());
out.println(„</P></BODY>”); out.println(„</HTML>”);
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
What is JSP ?
<HTML> <TITLE>Title</TITLE>; <BODY> <P> Today is:
<%= new java.util.Date().toString() %>
</P> </BODY></HTML>
<HTML> <TITLE>Title</TITLE>; <BODY> <P> Today is:
<%= new java.util.Date().toString() %>
</P> </BODY></HTML>
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
Elements of a JSP document
<%-- An example JSP page --%>
<%@page import=„java.util.*" contentType="text/html" session="true" %>
<HTML> <TITLE>Title</TITLE> <BODY> <P> Today is:
<%! String date; %>
<% date = new Date().toString(); %>
<%= date %> </P>
<jsp:include page="logo.jsp"/> </BODY></HTML>
<%-- An example JSP page --%>
<%@page import=„java.util.*" contentType="text/html" session="true" %>
<HTML> <TITLE>Title</TITLE> <BODY> <P> Today is:
<%! String date; %>
<% date = new Date().toString(); %>
<%= date %> </P>
<jsp:include page="logo.jsp"/> </BODY></HTML>
Directivespage – 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
timeActions 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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
JavaBeans in JSP (2)
<%@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>
<%@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>
What is JSP ?
Elements of a JSP document
JavaBeans and JSP
Exception handling
TagLibs, JSTL
JSP and Java Servlets
JSP v2.0
Summary
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
JavaBeans in JSP (3)
<%@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>
<%@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>
What is JSP ?
Elements of a JSP document
JavaBeans and JSP
Exception handling
TagLibs, JSTL
JSP and Java Servlets
JSP v2.0
Summary
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
JavaBeans in JSP (4)
<%@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>
<%@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>
What is JSP ?
Elements of a JSP document
JavaBeans and JSP
Exception handling
TagLibs, JSTL
JSP and Java Servlets
JSP v2.0
Summary
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
JavaBeans in JSP (5)
<%@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>
<%@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>
What is JSP ?
Elements of a JSP document
JavaBeans and JSP
Exception handling
TagLibs, JSTL
JSP and Java Servlets
JSP v2.0
Summary
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
<%@page contentType="text/html" errorPage="error.jsp" %>
<HTML> <TITLE>Test page</TITLE> <BODY> <P> <% ((String)null).length(); %> </P> </BODY></HTML>
<%@page contentType="text/html" errorPage="error.jsp" %>
<HTML> <TITLE>Test page</TITLE> <BODY> <P> <% ((String)null).length(); %> </P> </BODY></HTML>
<%@page contentType="text/html" isErrorPage="true" %>
<HTML> <TITLE>Error</TITLE> <BODY> <P> An error has occurred: <%= exception.getMessage() %> </P> </BODY></HTML>
<%@page contentType="text/html" isErrorPage="true" %>
<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
<%@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" %>
<%@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" %>
What is JSP ?
Elements of a JSP document
JavaBeans and JSP
Exception handling
TagLibs, JSTL
JSP and Java Servlets
JSP v2.0
Summary
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
Custom Tags (1)
<%@page import="org.stachoodev.examples.*" contentType="text/html" %>
<%@taglib prefix="util" uri="util.tld" %>
<%@include file="page_header.html" %>
<util:login request="<%= request %>" />
<%@include file="page_footer.html" %>
<%@page import="org.stachoodev.examples.*" contentType="text/html" %>
<%@taglib prefix="util" uri="util.tld" %>
<%@include file="page_header.html" %>
<util:login request="<%= request %>" />
<%@include file="page_footer.html" %>
What is JSP ?
Elements of a JSP document
JavaBeans and JSP
Exception handling
TagLibs, JSTL
JSP and Java Servlets
JSP v2.0
Summary
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
JSP and Java Servlets
<%@page import="java.util.*" %><HTML> <TITLE>Title</TITLE>; <BODY> <P> Today is:
<%= new Date().toString() %>
</P> </BODY></HTML>
<%@page import="java.util.*" %><HTML> <TITLE>Title</TITLE>; <BODY> <P> Today is:
<%= new Date().toString() %>
</P> </BODY></HTML>
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
<%@page import=„java.util.*" %>
<HTML> <TITLE>JSP test</TITLE> <BODY> <P> <%! String date; %> <% date = new Date().toString(); %> Today is: <%= date %> </P> </BODY></HTML>
<%@page import=„java.util.*" %>
<HTML> <TITLE>JSP test</TITLE> <BODY> <P> <%! String date; %> <% date = new Date().toString(); %> Today is: <%= date %> </P> </BODY></HTML>
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<HTML>\r\n<TITLE>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<HTML>\r\n<TITLE>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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
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
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
Summary
JavaServer Pages Homehttp://java.sun.com/jsp/
JSP Standard Templates Libraryhttp://java.sun.com/products/jsp/jstl/
Tomcat – Reference Implementation
http://jakarta.apache.org/tomcat
What is JSP ?
Elements of a JSP document
JavaBeans and JSP
Exception handling
TagLibs, JSTL
JSP and Java Servlets
JSP v2.0
Summary
JSP – A technology for serving dynamic web content Stanisław Osiński, 2002
Java Server PagesA technology for serving dynamic web content
Stanisław Osiński, stachoo@man.poznan.pl
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
top related