Beruflich Dokumente
Kultur Dokumente
Objectives
After completing this lesson, you should be able to do the following: Describe the relationship between JavaServer Pages (JSP) and servlets List implicit objects on JSP pages Describe the semantics of JSP tags Create a JSP segment Explain the use of JSP tag files Run and debug a JSP-based application
6-2
JavaServer Pages
Request Connects to
Dynamic content
Response
EJB
6-3
Servlets: Are Java programs with embedded HTML code Generate dynamic content Do not separate static and dynamic content
JavaServer Pages: Are HTML pages with embedded Java code or pure XML Generate dynamic content Separate static and dynamic content
6-4
Invoking JSPs
HTML
Invoke JSP
Servlet
JSP
6-5
Date JSP
<%@ page contentType="text/html;charset=WINDOWS-1252"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=WINDOWS-1252"> <title> Show Date </title> </head> <body> <h2> The current time is: </h2> <p> <%= new java.util.Date() %> </p> </body> </html>
6-6
Date Servlet
... public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Show Date </title></head><body><h2>The current time is:</h2><p>"); out.println(new java.util.Date()); out.println("</body></html>"); out.close(); } ...
6-7
6-8
http://host/date.jsp
6-9
A JSP contains three main elements: Text elements Directives Scripting elements
Declarations Expressions Scriptlets
6-10
Declarations
Are used to define methods or variables Begin with the sequence <%! End with the sequence %> Are inserted into the body of the servlet class, not within a method, during translation Are used in conjunction with expressions or scriptlets
<%! private int i=3; %> <%! private String a="Hello", b=" World"; %>
6-11
Expressions
Begin with the sequence <%= Contain Java expressions that are evaluated and inserted into the servlets output End with the sequence %> Do not end with a semicolon
6-13
Scriptlets
Begin with the sequence <% Contain a block of Java code that is executed every time a request is made End with the sequence %>
6-14
Implicit Objects
There are eight implicit objects, also known as predefined variables, in JSP: request application response config session pageContext out page
6-15
Example
6-17
Directives
Are used to set global values such as class declarations and method implementations Begin with the sequence <%@ End with the sequence %> Are of the following types:
page include taglib
6-19
include: Example
6-20
page Directive
You can define the following attributes by using the page directive: extends import info contentType errorPage isThreadSafe isErrorPage session language buffer autoFlush
6-21
package lesson04; import java.lang.*; import java.util.*; public class LuckyNumberBean { private int luckyNum; public LuckyNumberBean() { luckyNum = (int) (1000 * Math.random()); } public int getLuckyNum() { return luckyNum; } public void setLuckyNum(int luckyNum) { this.luckyNum = luckyNum; } }
6-23
6-24
Forward
Page 1
page scope
Page 2
page scope
Page 3
page scope request scope
6-27
6-29
Traditional: XML: It has no root element. <jsp:root> is the root element. <jsp:directive. page directive page/> <%@ page %>
Declaration tag <%! %> Expression tag <%= expression %> Scriptlet <% %>
6-30
JSP Segments
Use a JSP segment for creating a partial JSP page. Include one or more segments in a JSP using <jsp:include>. JSP segment footer.jspf:
Copyright 2005, <a href="http://www.oracle.com"> Oracle </a>
JSP:
<%@ include file="/footer.jspf"%>
6-32
Summary
In this lesson, you should have learned how to: Identify the differences and similarities between JSPs and servlets Use declarations, expressions, scriptlets, and implicit objects on JSP pages Use JavaBeans with JSPs Create a JSP segment Explain the use of JSP tag files Run and debug a JSP-based application
6-34
6-35