JSP Notes


JSP Directives (Directives are used during the translation phase; all other elements are used during request processing phase)

1.   JSP   : <%@ include file={static html file or regular jsp file} %> 
      XML: <jsp:directive.include file= "relativeURLspec" />
      Example:<%@ include file="file2.jsp" %>
      Description: includes file as part of the translation unit, which is compiled into a servlet; a page may include multiple include directives    

2. JSP   : <%@ page{…} %>
    XML: <jsp:directive.page pageDirectiveAttrList />
    Example:
    Import a Java class into the JSP page <%@ page import=”java.util.Date” %> (Multiple declarations are separated by comma)
    Declare that a JSP page exists within a session <%@ page session=”true”%>
    Declare that a JSP page uses an error page <%@ page errorPage=”errorPage.jsp” %>
    Declare that a JSP page is an error page <%@ page isErrorPage=”true” %>
    Description:Defines attributes that apply to a JSP page. assigns various page-dependent attributes; defaults are given below
     pageDirectiveAttrList:
    1.    language= “java”
    2.    contentType= “mimeType [;charset=characterSet] | text/html; charset=ISO-8859-1"
    3.    import=“java.util.Date, java.util.DateFormat”   {may have more than one importassignments via multiple page directives}
    4.    session=“true | false (if set to false, the implicit session variable is not available to scripting elements)
    5.    isErrorPage= “false | true (set to true if this page is used as anerror page, and it will have the implicit exception variable available to scripting elements)
    6.    errorPage=“relativeURL" (no default; page-relative or context-relative URI to redirect toin case exception thrown)
    7.    autoFlush=“true | false   (if false, throws exception when buffer is full)
    8.    buffer=“8kb | none | sizekb(defines size of buffer; “none” to disable buffering)
    9.    info=“text" (no default; used by server administrative tool as a description ofthe jsp page)
  10.    isThreadSafe= “true | false (if set to false, then will useSingleThreadModel)
  11.    extends=“package.class" (no default; fully qualified classname to extend; class must implement JspPage or HttpJspPage interface)
 

3. JSP  : <%@ taglib uri="URI" prefix="tagPrefix" %>
   XML: No equivlalvent included in Root
   Example : <%@taglib uri="/tomcat/taglib" prefix="test">
   Description:
    1. declares a tag library, containing custom actions, that is used in the page
   1.    prefix= “tagPrefix" (mandatory; no default; specifies prefix to use in the action element names for all actions in thelibrary)
   2.    uri=“URI” (mandatory; no default; either a symbolic name for the tag libraryin the web.xml file or a page- or context-relative uri for the library’sTLD or JAR file)

JSP Scripting Element - Declaration
   JSP  :  <% code fragment %>
   XML: <jsp:scriptlet> code fragment </jsp:scriptlet>

   Example: <%! String mystring = new String(“hello”); %>
   Description:
    1.must be a valid java variable declaration; 
    2.declares instance variables of the JSP implementation class;
    3.thread-safe alternative is to declare variables in scriptlets, so they are local variables
    4.may also declare methods within a Declaration element
    5.JSP implicit variables are not visible in a declaration element, since they are declared within the _jspService() method.

JSP Scripting Element - Expression
    JSP   : <%= expression %>
    XML:<jsp:expression>expression </jsp:expression>

    Example:   <%= 1+2 %> or <%= mystring %>
    Description:       
     1. must be complete valid java expression that results in or can be converted to a string.
     2. all JSPimplicit variables are visible in an expression element

JSP Scripting Element - Scriptlet
    JSP   <% code fragment %>
    XML<jsp:scriptlet>code fragment</jsp:scriptlet>
    Example: <% if (1+2==true) { %> yippee!! <% } else { %> boohoo!! <% } %>

    Description:
     1.scriptletcode fragments are combined with code for sending template data between them tothe browser; the combination of all scriptlets in a page must form valid javacode
     2.all JSPimplicit variables are visible in a scriptlet element


a) Directive - <%@ Directive {attribute=”value”}* %>   --controls translation and compilation phase.
b) Declaration - <%! JavaDeclaration %>                         --declare a java variable or method. No output produced. Class level declarations.
c) Scriptlet - <% JavaStatements %>                                --valid java statements. No output produced unless out is used
d) Expression - <%= JavaExpression %>                          --evaluation of expression to a String and then included in the output. 


JSP Action Elements
<jsp:forward>
JSP    : <jsp:forward page="{relativeURL | <%= expression %> }" { /> | >
                   [ <jsp:param name="parameterName" value="{parameterValue
                                                               | <%= expression %>}" /> ] + </jsp:forward> }
XML  : Same JSP Syntax

Example:<jsp:forward page= {page- or context-relative uri of jsp or servlet} />
 uses RequestDispatcher to forward control to another resource; see RequestDispatcher.forward restrictions

<jsp:include> Includes a static file or the result from another web component.

JSP   <jsp:include page="{relativeURL | <%= expression %>}" [ flush="true | false" ]{ /> | >
                    [ <jsp:param name="parameterName" value="{ parameterValue
                                                             | <%= expression %>}" /> ] +</jsp:include> }
XMLSame JSP Syntax


Example: <jsp:include page= {page- or context-relative uri of jsp or servlet} flush= “true” />
 uses RequestDispatcher to include output from another resource; see RequestDispatcher.include restrictions

<jsp:useBean> Locates or instantiates a bean with a specific name and scope.
JSP   <jsp:useBean id="beanInstanceName" scope=" page|request|session|application"
         { class="package.class" [ type="package.class" ] | beanName="{package.class | <%= expression %>}"
                                                type="package.class" |type="package.class "}{ /> | > other elements </jsp:useBean> }
XMLSame JSP Syntax

3.    <jsp:useBean{…} > 

 associates a java bean with a name in one of the JSP scopes and also makes it available as a scripting variable; instantiates the bean if it doesn’talready exist as the given id
    class = {fully qualified classname for the bean}
    id ={java variable name to use}-- variable name is made available via <jsp:getProperty..> as well as a scripting variable
    scope ={page(default), request, session or application}-- stores bean object in pageContext, request, session or context attribute,respectively
    type ={optional; defines a type to which the bean should be cast; class must be a subclass of type or, if type is an interface, class must implement type}
    beanName ={optional; the beanName parameter as expected by the java.beans.Beans.instantiate() method}
    Note:jsp:useBean action element can have a non-empty body, e.g. a jsp:setProperty action; the body will be executed for newly instantiated beans only

4.<jsp:setProperty> Sets a bean property value or values.
JSP   <jsp:setProperty name="beanInstanceName "
          { property="*" | property="propertyName " [ param="parameterName" ] | property="propertyName " value="{string | <%= expression%>}" } />
XML Same JSP Syntax
Example:
<jsp:setProperty {…} />  -- sets value of myClock’s property(s)
        name= “myClock”
        property=“date” (if  “*”, then will set all properties with names matchingrequest parameters)
        param=“…” (optional; explicitly specifies a request parameter that holdsvalue to use for specified property)
        value=“…” (optional: explicitly specifies a value to assign to the property;value cannot be used in conjunction with param; value can be set equal to an expression e.g. value= “<%= newjava.util.Date()%>”)
         if property value is a String, it is converted to the target property’stype, e.g. Float.valueOf(String)
5.<jsp:getProperty> Inserts the value of a bean property into the result.
JSP   <jsp:getProperty name="beanInstanceName "property="propertyName" />
XML Same JSP Syntax

Example :  <jsp:getProperty name= “myClock” property= “date” /> -- same as the expression <%=myClock.date %>

JSP Implicit Variables
1.    request: the HttpServletRequest object for  the current request
2.    response: the HttpServletResponse object for the current request
3.    session: the HttpSession as per request.getSession(); you may call session.invalidate()to implement a logout
4.    application:the ServletContext as per GenericServlet.getServletContext(); application.log()can be used to write to the application log file
5.    out:assigned by web container to a concrete subclass of JspWriter (which emulatessome of the functionality found PrintWriter and BufferedWriter, but throwsIOException unlike PrintWriter)
6.    exception:only available in error pages
7.    config: theServletConfig as returned by GenericServlet.getServletConfig()ß used by web container to pass info to servlet or JSP duringinitialization (e.g init parameters, context, servletname)
8.    pageContext:used by the container; a pageContext instance provides access to all of thescopes associated with a JSP page, provides access to several page attributesandall the JSP implicit variables. A unique instance of this object is created bythe web container and assigned to the pageContext variable for each request, andis used in the JSP implementation class (servlet) to initialize the JSP implicitvariables.
9.    page:assigned to the instance of the JSP implementation class (servlet) declared asan Object


JSP Page Lifecycle
1.    The Translation Phase includes the following and is initiated either
         (1) whenthe first request for a new or updated JSP page is received, or
         (2) at deployment time, depending on the application server
2.    Page Translation: The JSP page is translated into Java servlet code.
3.    JSP PageCompilation: The servlet code is compiled into a JSP pageimplementation class.
4.    The Execution / Request-Processing Phase includes the following and is the sameas for a regular servlet.
5.    Load Class: The JSP page implementation class is loaded by the JVM.
6.    CreateInstance: The JSP page implementation class is instantiated.
7.    Call jspInit: jsp_init() is invoked when the JspPage is initialized. At this pointgetServletConfig() will return the desired value.
8.    Call _jspService: _jspService corresponds to the body of the JSP page. This method isdefined automatically by the JSP processor and should NEVER BE DEFINED BY THEJSP AUTHOR.
9.    Call jspDestroy: jsp_destroy() is invoked when the JspPage is about to be destroyed.









Hosted by www.Geocities.ws

1