Download presentation
Presentation is loading. Please wait.
Published byNigel Warner Modified over 9 years ago
1
Chapter 7 Java Server Pages
2
Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP List and describe the JSP element types Explain the operation of a JSP error page Explain the operation of include and forward directives Explain the purpose of a Java Bean and how one can be used from a JSP Explain the purpose of a taglib and how a tag can be used from a JSP
3
Separation of Concerns A basic principle of software design: Software modules should have distinct responsibilities as much as possible input calculation data retrieval output
4
Separation of Content From Processing (1/2) A servlet generating an HTML document intermixes Java code and HTML public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException() { PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println(" " + "Example HTML" + " ";... out.println(" "); out.close(); }
5
Separation of Content From Processing (2/2) Java Server Pages (JSP) allow static HTML content to be separated from dynamic content (created by Java code) within a document Example Page Example Page Hello,...
6
JSP Elements JSP tags belong to one of five element types Declaration:declare variable or procedure Expression:evaluate a Java expression Scriptlet:execute a Java code segment Directive:a JSP compiler command Action Element: interacts with another component
7
JSP Declaration A declaration can be used to initialize variables or define procedures for use later in the JSP <%! // return the difference between two integers public int diff(int a, int b) { return (a < b) ? (b - a) : (a – b); } %>
8
JSP Expression A JSP expression is evaluated and then replaced by that value Today is: Generated HTML: Today is: Mon Jun 15 08:05:00 EST 2009
9
JSP Scriptlet A JSP scriptlet is a short section of Java code that is executed in-line Scriptlets have access to several pseudovariables: –outHTML output stream –sessioncurrent HttpSession object –requestHTTPRequest object –responseHTTPResponse object
10
JSP Scriptlet This scriptlet displays the current date and time <% // output the current date and time Date today = new Date(); out.println(" Today is: " + date.toString() + " "; %>
11
JSP Scriptlet The scriptlet checks to see if a Boolean session attributed named "login" exists <% // check to see if the user is logged in boolean loggedIn = ((Boolean) session.getAttribute("login")).booleanValue(); if (loggedIn) { out.println(" Please select an option "; } else { out.println(" Please login "; } %>
12
JSP Scriptlet The scriptlet maintains a "hit count" <% // get or create hit count Integer hitCount = (Integer) session.getAttribute("hits"); if (hitCount == null) { hitCount = new Integer(0): } // increment and save hit count hitCount = new Integer(hitCount.intValue() + 1); session.setAttribute("hits", hitCount); // display hit count Hit Count: %>
13
JSP Life-Cycle JSP's are actually executed as servlets The first time a JSP is requested, it is translated to a servlet and compiled Each subsequent request executes the same servlet JSP Servlet translated upon first request
14
JSP Life-Cycle
15
JSP Directive The page directive appears at the top of each JSP and controls its execution The page directive also specifies imports
16
JSP Errors Compilation errors and run-time exceptions are logged and then returned as an HTML response ???
17
JSP Error Page The proper way to handle errors is to declare an error page The error page is invoked whenever a JSP compilation or run-time error occurs The error page can display a standard error message
18
JSP Error Page JSP that might throw an error:... possible errors... stderror.jsp: Error Page That request cannot be completed at this time. Please contact your system administrator.
19
JSP Action Element JSP action elements control interactions with other JSP's, including –include: add the contents of another JSP –forward: send a request to another JSP for handling
20
JSP Include A JSP include can be used to add standard content to a JSP, such as headers and footers...unique content of this JSP...
21
JSP Forward A JSP forward causes control to be transferred to a different JSP the scriptlet is continued after the directive, which is not a Java statement
22
Java Beans A Java Bean is a Java class that can be invoked from a JSP This allows complex processing required by a JSP to be separated from the JSP A JSP can instantiate an instance of a bean class and invoke its methods
23
Java Bean Example Java Bean package bean; public class AdderUtility { public int add(int a, int b) { return a + b; } JSP <% int a, b, sum; … int sum = adder.add(a, b); %>
24
Java Bean Uses Java Beans are typically used to encapsulate complex application logic, including calculations, database access, or network access JSP Java Bean Database Network Application
25
JSP Taglibs A tag library allows programmers to create custom tags to invoke application functions with JSPs Each custom tag is associated with a Java class that is executed when the tag is evaluated
26
Taglib Example JSP code: This code appears in a green box This code appears in a green box tag body content
27
Taglib Example BoxInTag.java public class BoxInTag extends BodyTagSupport { private String border; public void setBorder(String border) { this.border = border; } public int doStartTag() throws JspTagException { return EVAL_BODY_BUFFERED; } public int doEndTag() throws JspTagException { JspWriter out = pageContext.getOut(); String content = bodyContent.getString(); try { out.println("<div style=\"border-style: solid; border-color: " + border + "\">" + content + " " ); } catch (IOException ioe) { ioe.printStackTrace(); } return EVAL_PAGE; } returns the tag body content, i.e., the text between the tag opening and closing
28
Tag Library Definition File (TLD) <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance“ xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> 1.0 Show Standings Tag boxin tags.BoxInTag Draw a box around the content tagdependent
29
Review Java Server Pages JSP life-cycle JSP Elements JSP include and forward directives Java Beans with JSP JSP taglibs
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.