JSP Architecture JSP is a simple text file consisting of HTML or XML content along with JSP elements JSP packages define the interface for the compiled JSP page JSPPage HttpJspPage Three main methods jspInit() jspDestroy() _jspService(HttpServletRequest request, HttpServletResponse response)
Elements of a JavaServer Page JSP Elements “Template Text” – HTML When request is processed template text & dynamic content merged result sent as response to browser JSP Elements Directive Action Scripting
Elements of a JavaServer Page Directives provide global information to the page Action perform action based on up-to-date information Scripting Declarative for page-wide variable and method declaration Scriptlets the Java code embedded in the page Expressions Format the expression as a string for inclusion in the output of the page.
JSP Syntax HTML Comment Comment can be viewed in the HTML source file --> Example: <!-- This page was loaded on <%= (new java.util.Date ()).toLocaleString()%> --> View source:
JSP Syntax Hidden Comment Everything between the is ignored by the JSP container Comment cannot be viewed in the HTML source file Example: A Test of Comments
JSP Directives A JSP directive is a statement that gives the JSP engine information for the page. Used to provide information about the general set-up of the page. e.g. inclusion of header files, name of page to report errors, whether session tracking is required etc Syntax For Example: scripting language used session tracking required name of page for error reporting
Types of Directive Elements Define page-dependent attributes, such as scripting language, error page & buffering requires Include a file during the translation phase. Declares a tag library, containing custom actions, used in the page Each directive has a set of associated attributes (similar to some tags in HTML) Full list of attributes available at:
JSP Syntax Include Directive Includes a static file Example: main.jsp: Current date and time is: date.jsp: Output : Current date and time is: Mar 5, :56:50
Attribute for the page directive and possible values language -- The language attribute defines the scripting language to be used in the page. The value "java" is the only value currently defined and is the default. extends -- The extends attribute defines the (fully-qualified) class name of the superclass of the servlet class that is generated from this JSP page. import -- The import attribute defines the set of classes and packages that must be imported in the servlet class definition.
Attribute for the page directive and possible values session -- The session attribute defines whether the JSP page is participating in an HTTP session. The value is either true (the default) or false. Buffer -- The buffer attribute defines the size of the buffer used in the output stream (a JspWriter object). The value is either none or Nkb. autoFlush -- The autoFlush attribute defines whether the buffer output should be flushed automatically when the buffer is filled or whether an exception is thrown. The value is either true (automatically flush) or false (throw an exception). The default is true.
Attribute for the page directive and possible values isThreadSafe -- The isThreadSafe attribute defines whether the JSP servlet implements the SingleThreadModel interface. The value is either true or false. The default is true. Info -- The info attribute defines an informational string about the JSP page. errorPage -- The errorPage attribute indicates another JSP page that will handle all runtime exceptions thrown by this JSP page. The value is a URL that is either relative to the current Web hierarchy or relative to the context root. For example, errorPage="error.jsp" (this is relative to the current hierarchy) or errorPage="/error/formErrors.jsp" (this is relative to the Web application's context root).
Attribute for the page directive and possible values isErrorPage -- The isErrorPage attribute defines that the JSP page has been designed to be the target of another JSP page's errorPage attribute. The value is either true or false (default). All JSP pages that "are an error page" automatically have access to the exception implicit variable.
JSP action elements Action elements are an important syntax element in JSP They are represented by tags (as is HTML) They assist JSP developers to develop in tags rather than scriplet programming Instead of <%, they just use the < character (like HTML) For example: body
JSP action elements Full syntax of JSP Action Elements is: action_body If the element doesn’t have a body, can lose the end tag and use shorthand syntax of:
JSP action elements There are two types of JSP action elements: 1. JSP Pre – defined tags, also called Standarded Action Elements. 2. External tag library: Custom or JSTL(Java Standard tag library)
JSP standard action elements jsp:useBean jsp:setProperty jsp:getProperty jsp:param jsp:include jsp:forward jsp:plugin jsp:params jsp:fallback
JSP standard action elements Standard Action Example: tag Example: Going to include hellouser.jsp... Executes the included JSP page and adds its output into the this page
JSP standard action elements What’s difference from using the “include” derective? The include directive includes the contents of another file at compilation time. Good for including common static code e.g. header file, footer file. Good on performance included only once. But, what if including dynamic common code (e.g. a navigation bar where links are read from the dB?).. need to re-run the file each time a request is made JSP: include
JSP standard action elements Standard Action Example: tag Example: Error occurred…please wait Stops processing of one page and starts processing the page specified by the page attribute
JSP Declaration The definition of class-level variables and methods Syntax Example <%! String var1 = “hi”; int count = 0; private void incrementCount() { count++; } %>
JSP Scriptlets Scriptlets are defined as any block of valid Java code that resides between tags This code will placed in the generated servlet _jspService() method. Example <% String var1 = request.getParameter(“lname”); out.println(var1); %>
JSP Expressions A JSP expression is a nice tool for embedding values within your HTML code. Syntax Example Counter value is
Scripting: Implicit JSP Objects Request -- The HttpServletRequest object associated with the request Response -- The HttpServletResponse object associated with the response that is sent back to the browser pageContext -- This object encapsulates the environment of a single request for this JSP page Session -- The HttpSession object associated with the session for the given user of the request. This variable is only meaningful if the JSP page is participating in an HTTP session
Scripting: Implicit JSP Objects Application -- The ServletContext object for the Web application Out -- The JspWriter object associated with the output stream of the response Config -- The ServletConfig object associated with the servlet for this JSP page Exception -- The Throwable object that was thrown by some other JSP page. This variable is only available in a "JSP error page."
Scripting: Implicit JSP Objects Example Use Out <% //print a simple message using the implicit out object out.println( " Hello! " ); %>
Use of Implicit Objects… The following information was part of this request Request method Request URI Request Protocal Server Name Server Port Remote Address Remote Host
Use of Implicit Objects… Example Use Request <% //print a simple message using the implicit out object out.println( " Hello! " + request.getParameter("user") + " " ); %>