CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
-
Upload
delilah-robinson -
Category
Documents
-
view
214 -
download
0
Transcript of CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
CSC 2720Building Web Applications
JavaServer Pages (JSP)JSP Directives and Action Elements
JSP Directives page directives – defines attributes that apply to an entire
JSP page
include directives – includes a resource of text or code when the JSP page is translated
taglib directives – defines a tag library and prefix for the custom tags used in the JSP page
Syntax: <%@ directive attribute="value" %> <%@ directive attribute1="value1" attribute2="value2"
... %>where directive can be page, include, or taglib
page Directives Defines attributes that apply to an entire JSP page.
Which classes are imported
For examples, To specify a different superclass for the current JSP
page To specify the MIME type of the generated content To indicate if the JSP page is thread safe or not To turn on/off automatic session participation To change the size and behavior of the output buffer To indicate which page is to be used to handle
unexpected errors/exceptions
page Directives – import Syntax
<%@ page import="package.class" %> <%@ page import="package1.class1,…,packageM.classN" %>
Note: To import all classes from a package, use package.*
Purpose Generate import statements at for the resulting servlet class
Note: Class files should be placed under the folder …/Web_App_Name/WEB-INF/classesand in the appropriate sub-folders that match the package name.
.jar library files should be placed under the folder…/Web_App_Name/WEB-INF/lib
page Directives – contentType Syntax
<%@ page contentType="MIME-Type" %>
Purpose Specify the MIME type of the content generated by the JSP page Default value is "text/html;charset=ISO-8859-1"
e.g.: HTML content encoded in simplified Chinese characters <%@ page contentType="text/html;charset=GB2312">
References Wiki: MIME: http://en.wikipedia.org/wiki/MIME MIME reference: http://www.w3schools.com/media/media_mimeref.asp Character set reference: http://www.iana.org/assignments/character-sets
<%@ page contentType="application/vnd.ms-excel" %>
First Last Email Address
Marty Hall [email protected]
Larry Brown [email protected]
Bill Gates [email protected]
Larry Ellison [email protected]
<%-- Note: The values are separated by tabs and not by spaces
--%>
Generating Excel Spreadsheets
Other Attributes of the page Directive session
Lets you turn on/off session participation Default value is "true" e.g.: <%@ page session="false" %>
extends Changes parent class of the resulting servlet
isThreadSafe Lets you specify whether the JSP page is thread safe Default value is "true" Setting this attribute to "false" makes the resulting servlet a single-
threaded servlet e.g.: <%@ page isThreadSafe="false" %>
Other Attributes of the page Directive language
Let you specify the scripting language Default value is "java"
info Allows you to insert a string that later can be retrieved using the getServletInfo() method.
e.g.: <%@ page info="Written by XYZ" %>
buffer Changes the minimum size of buffer used by JspWriter The unit of the size is in kilobyte e.g.: <%@ page buffer="none" %> means don't buffer the
output e.g.: <%@ page buffer="12" %> means set buffer size to
12kbytes
autoflush Requires the developer to explicitly flush buffer
Other Attributes of the page Directive errorPage
Designates a page to handle unplanned errors (i.e., when an exception is uncaught in the JSP page)
e.g.: <%@ page errorPage="MyErrorPage.jsp" %>
isErrorPage Indicates if the current page is a page designated for handling error Default value is "false" If "true", the "exception" implicit object is made available to this
page. You can find out from the "exception" object which JSP page is throwing
an exception and what kind of exception it is.
Note: We can also configure error pages in the web.xml (web application deployment file).
A Note On Directive Elements Except for "import", other attributes cannot repeat.
The following is illegal
<%@ page buffer="16384" %>
<%@ page buffer="8192" %>
The following is illegal
<%@ page buffer="16384" session="false"
buffer="8192" %>
The following is legal
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
The include Directive Static include – to include the contents of other files in the
current JSP page at translation time.
Syntax: <%@ include file="relativeURL" %>
// Content A<%@ include file="x.html" %>// Content B
// Content X
// Content A// Content X// Content B
Translated into Servlet codes
original.jsp x.html
Notes: Servers may not detect changes made to the included files. Thus, you need to update the "last modified date" of the JSP files whenever the included files change.
The include Directive
Purposes To reuse JSP content in multiple pages
e.g.: Menus, headers, footers, etc.
When to use If the included file contains static text If the included file is rarely changed To include files (including JSP fragment files) that are placed un
der the /WEB-INF folder.
Shortcoming Lack of locality (The main file and all the included files share the
same scope difficult to debug) Not suitable for files that’s are too big. (A Java method cannot
exceeds 64kbytes in size.)
Standard Action Elements Special Tags that can be embedded in a JSP page
At compile time, these tags are also converted into corresponding Java codes.
7 standard JSP action elements: jsp:include, jsp:forward, jsp:param
More commonly used ones
jsp:useBean, jsp:setProperty, jsp:getProperty For used with JSP Bean (Will discussed later)
jsp:plugin For generating <OBJECT> or <EMBED> tags for Java applet.
jsp:include Action Dynamic include – To incorporate static or dynamic
resources into the current page at request time. Similar to using a RequestDispatcher object to include a resource
Can pass data to the included resources
Syntax<jsp:include page="anotherURL" flush="true" />
or<jsp:include page="anotherURL" flush="true"> <jsp:param name="name1" value="value1" /> …
</jsp:include>
jsp:include Action
// Content A<jsp:include page="x.html" />// Content B
// Content X
// Java codes to produce content ApageContext.include("x.html");// Java codes to produce content B
Translated into servlet codes
original.jsp x.html
Java byte codes
Compiled into Java byte codes
Executed by web container
Included into the output stream at rune time.
If the included resource is a JSP file, the output produced by the JSP file is included in the output stream of original.jsp.
jsp:include Action
Advantages over the include directive Easier to maintain
Changes made to the included files automatically reflected in the files that include them.
Easier to debug (Strong locality)
Shortcoming Slower Cannot include files placed in /WEB-INF folder
When to use If the file is subject to modification very often If the size of the file is huge
Include Example – Menu
<table border="0" cellpadding="0" width="100%"><tr><td>Menu Item 1</td></tr><tr><td>Menu Item 2</td></tr><tr><td>Menu Item 3</td></tr></table>
<%@page contentType="text/html" pageEncoding="UTF-8"%><html><head><title>JSP Menu Demo</title></head><body><table border="0" width="100%" cellpadding="0" cellspacing="0" ><tr><td><%@ include file="menu.html" %></td></tr><tr><td bgcolor="#aacccc">Contents goes here ...</td></tr></table></body></html>
menu.html (note: With include directive, you can give any file extension to this file.)
In this example, using <jsp:include …> produces same output
Reusable JSP Content: ContactSection.jsp<%@ page import="java.util.Date" %><%-- The following become fields in each servlet object that results from a JSP page that includes this file. --%><%! private int accessCount = 0;private Date accessDate = new Date();private String accessHost = "<I>No previous access</I>";%><hr />© 2000 <a href="http://www.my-company.com/">my-company.com</a>.This page has been accessed <%= ++accessCount %>times since server reboot. It was last accessed from <%= accessHost %> at <%= accessDate %>.<% accessHost = request.getRemoteHost(); %><% accessDate = new Date(); %>
…<BODY><TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Some Random Page</TABLE><P> Information about our products and services.<P> Blah, blah, blah.<P> Yadda, yadda, yadda.<%@ include file="ContactSection.jsp" %></BODY></HTML>
Using the JSP Content
jsp:forward Action Used to instruct a web server to stop processing
the current page and start another one. Similar to using a RequestDispatcher object to forward
a request
Syntax<jsp:forward page="anotherURL" />
or<jsp:forward page="anotherURL">
<jsp:param name="param1" value="value1">
…
</jsp:forward>
Note: For both jsp:include and jsp:forward
The parameters passed to the included/forwarded resources can be obtained asrequest.getParameter("paramName")
If the value of page attribute begins with '/', then the path is evaluated relative to the application root folder. Otherwise the path is evaluated relative to the current folder.
References Wikipedia: JavaServer Pages
http://en.wikipedia.org/wiki/JavaServer_Pages Free Tutorial (Java, JSP, Java Servlets)
http://www.courses.coreservlets.com/Course-Materials/ Sample JSP codes
http://www.java2s.com/Code/Java/JSP/CatalogJSP.htm