Last Updated: December 28, 2002 JSP 1.2 (Java Server Pages) =========================== Quick Introduction ------------------ JSPs are designed to be online with the J2EE model of separating static and dynamic content - separating presentation and logic - in a web application. It's primary objective is to simplfy dynamic presentation in a multi-tiered architecture. MVC is at play here: Model View Controller. Described by Xerox: Model corresponds to business logic and data. Business logic manipulates applicaiton data such as customer, product, and order information. things. View is the presentation logic. Presentation refers to how the application is displayed to the user. Controller is the request processing. The request processer is what ties the Model (biz logic) and View (presentation) together. Why MVC? Model tends to be stable, while the View changes often. JSP can be used for the Controller and the View while JavaBean components are used by the Model. The Beef -------- IMPORTANT: Even though JSPs contain special scripting tags, under the hood they are compiled as servlets. Quick example of a JSP page: <%@page language="java" contextType="text/html"%> The current time is <%= new Date().toString() %> <%-- Comments look like this. Comments are hidden after the JSP is compiled --%> JSP action element is used to access a bean. JSP defines standard actions as well as how you can define custom actions. Basic Notation: action body All JSP action elements use prefix jsp. Custom actions cannot use prefixes of: jsp, jspx, java, javax, servlet, sun, sunw Certain attributes can have expressions in them Attribute Attribute where expression that can be included jsp.setProperty name,value jsp.include page jsp.forward page jsp.param value Page Directives <%@ page ... %> ------------------------------- <%@ page import="java.io.*" errorPage="catcher.jsp" %> Beans ----- - id attribute gives the bean a unique name -> must be valid Java variable name! - When action has no body must close with /> (like XML) Once you create your own beans you can retrieve bean values with: A Bean is a class with a constructor with no arguments. Properties have accessors that allow access to the property. As in: getMonth setMonth Scriptlets <% %> ---------------- Called by service(), are inserted into the servlets _jspservice method. <%-- JSP scriptlet --%> <% if (clock.getHours() < 12) { %> Good Morning <% } else if (clock.getHours() < 17 } %> Good Afternoon ... Variables in scriptlets are local variables not shared by all instances of the page. <% String[]picked = request.getParameterValues("fruits"); if (picked != null && picked length !=0 \} %> You picked the following fruits:
    <% for (int i=0; i < picked.length; i++) { out.println(""
  • "+ picked[i]); } // end for } // end if %>
Declarations <%! code %> ------------------------ <%! int height = 0; %> Declations are inserted into body of servlet class outside of any existing methods. If you want <% in the output, escape it "<\%" JSP Expressions <%= expression %> (to print them) ------------------------------------------------- Implicit JSP objects / Predefined Variables: request java.servlet.http response java.servlet.http pageContext java.servlet.jsp session java.servlet.http out java.servlet.servletContext config java.servlet.servletConfig page java.lang.Object exception java.lang.Throwable Expressions are evaluated and inserted into the page output. <%= userInfo.getUserName() %> // must not end with semicolon! These are syntactically equivalent: "> So is this: <%@pageimport="com.ora.jsp.util.*"%> <%@pageimport="java.util.*"%> To: <%@pageimport="java.util.*,com.ora.jsp.util.*"%> JSP Variable and Method Declarations (Avoid in general) ------------------------------------------------------- <%! int globalCounter=0; %> globalCounter will be shared by all instances of the page. If you declare variables or methods with <%! %> you can use: jspInit(), jspDestroy() <%! int globalCounter=0; public void jspInit() { startDate=newDate(); } public void jspDestroy() { } %> Passing Control --------------- Target page has all request information forwarded. You may also add additional params via: Scopes ------ Page (default) - available only to single page Request - available to all pages processing same request Session - shared by multiple requests by same user Application - shared by all users -- setProperty declared in this fashion will only set property at bean creation time -- Request Time Attribute Values Not all JSP actions support / accept request time attributes. File Includes ------------- <%@ include file="relative url"%> Include directive here inserted at page translation (first call) not at request time! Thus these files can contain JSP. If this JSP file changes, all included files must change (% touch *.jsp) Includes a jsp page at request time. Tag Libraries / Developing Custom JSP Actions ============================================= A bean with property setter methods corresponding to the custom action element's attributes. The bean must implement one of two java interfaces defined by the JSP specification All interfaces / classes you need to implement are in javax.servlet.jsp.tagext Two primary interfaces Tag - defines methods you need to implement for any action BodyTag - extends Tag and adds methods to access the body of an action element Tag Interface <---(implements)-- TagSupportClass ^ ^ | (extends) | (extends) BodyTagInterface <----------------- BodyTagSupport Class Custom actions (tag handler class files) packaged form a tag library. This package needs a TLD (Tag Library Descriptor). This is put into a .tld file in WEB-INF/ public class HelloTag extends TagSupport { private String name="world"; public void setName(String name) { this.name=name; } // doEndTag defined by Tag Interface public int doEndTag() { try { pageContext.getOut().println("Hello" + name); } } //end doEndTag() } // end class Put the class in WEB-INF/classes then write TLD file ... hello com.mycompany.HelloTag empty name Now test the custom action in a JSP page. A custom action consists of a start tag, a body and an end tag body If there is no body you can end with attr2="value2 <%-- 2) setAttr2("value2") --%> <%-- 3) doStartTag() --%> body <%-- 4) doEndTag() --%> TagHandlerObjects can be reused by JSP NOTE: TagHandler cannot reset instance variables until release(), otherwise behavior is undefined.