jsp architecture
DESCRIPTION
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, - PowerPoint PPT PresentationTRANSCRIPT
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 processedtemplate text & dynamic content mergedresult sent as response to browser
JSP ElementsDirectiveActionScripting
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
<!-- comment <% expression%> -->
Example:
<!-- this is just Html comment --><!-- This page was loaded on <%= (new java.util.Date()).toLocaleString()%> -->
View source:
<!-- this is just Html comment --><!-- This page was loaded on January 1, 2002 -->
JSP Syntax Hidden Comment
Everything between the <%-- and --%> is ignored by the JSP containerComment cannot be viewed in the HTML sourcefile
<% -- expression -- %>
Example:
<html><body><h2>A Test of Comments</h2><%--This comment will be invisible in page source --%></body></html>
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<%@ directive {attribute= “value” } %>
For Example:scripting language usedsession tracking requiredname of page for error reporting
Types of Directive Elements
<%@ page ... %> Define page-dependent attributes, such as scripting language, error page & buffering requires
<%@ include ... %> Include a file during the translation phase.
<%@ taglib ... %> 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: http://java.sun.com/products/jsp/syntax/1.1/syntaxref11.html
JSP Syntax Include Directive
Includes a static file<%@ include file=“relativeURL” %>
Example:main.jsp: <html><body>
Current date and time is:<%@include file=“date.jsp” %></body></html>
date.jsp: <%@page import =“java.util.*” %><% =(new java.util.Date()).toLocaleString()
%>
Output : Current date and time is:Mar 5, 2000 4: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) orerrorPage="/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 JSPThey are represented by tags (as is HTML)They assist JSP developers to develop in tags rather than scriplet programmingInstead of <%, they just use the < character (like HTML)
For example: <prefix:action_name>
body </prefix:action_name >
JSP action elements
Full syntax of JSP Action Elements is: <prefix:action_name attr1 = “value” attr2 =
“value2”> action_body
</prefix:action_name>If the element doesn’t have a body, can lose the end tag and use shorthand syntax of:
<prefix:action_name attr1 = “value” attr2 = “value2” />
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:useBeanjsp:setPropertyjsp:getPropertyjsp:paramjsp:includejsp:forwardjsp:pluginjsp:paramsjsp:fallback
JSP standard action elements
Standard Action Example: <JSP: include> tagExample:
<HTML> <BODY>
Going to include hellouser.jsp...<BR>
<jsp:include page="hellouser.jsp"/>
</BODY>
</HTML>
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: <JSP: forward> tagExample:
<HTML> <BODY>
Error occurred…please wait<BR>
<jsp:forward page=“errorhandle.jsp"/>
</BODY>
</HTML>
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<%! Declaration %>
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 <% and %> 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<%= expression %>
Example
<% for (int i = 0; i < 10; i++) { %><BR>Counter value is <%= i %><% } %>
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 theresponse
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<%@ page errorPage="errorpage.jsp"%><HTML><HEAD><TITLE>Use Out</TITLE></HEAD><BODY>
<%//print a simple message using the implicit out
object out.println( "<center><b>Hello!</b></center>" );
%>
</BODY></HTML>
Use of Implicit Objects…
The following information was part of this request
<li> Request method <%= request.getMethod() %>
<li> Request URI <%= request.getRequestURI() %>
<li> Request Protocal <%= request.getProtocol() %>
<li> Server Name <%= request.getServerName() %>
<li> Server Port <%= request.getServerPort() %>
<li> Remote Address <%= request.getRemoteAddr() %>
<li> Remote Host <%= request.getRemoteHost() %>
Use of Implicit Objects…
Example<%@ page errorPage="errorpage.jsp"%><HTML><HEAD><TITLE>Use Request</TITLE></HEAD><BODY>
<%//print a simple message using the implicit out
object out.println( "<b>Hello!</b>" + request.getParameter("user") + "</b>" );
%>
</BODY></HTML>