Contents [0/12] |
Overview of Today's Class [1/12] |
Request Dispatching [2/12] |
JSP/Servlet Differences [3/12] |
Webapp components [4/12] |
Implicit Objects and their Scope [5/12] |
JSP Directives [6/12] |
JSP Actions [7/12] |
JavaBeans [8/12] |
JavaBean basics [9/12] |
Using JavaBeans in JSPs [10/12] |
Using JavaBeans in JSPs, manipulating data [11/12] |
Homework [12/12] |
Overview of Today's Class [1/12] |
Request Dispatching
JSP details
JavaBeans
Integrating Servlets with JSPs
Request Dispatching [2/12] |
Often, when building a web application, you want to forward processing to another component. Or, you might want to include the processing of another component in the current response.
In order to do this using the Servlet API, we need to use a Request Dispatcher.
This is retrieved from the ServletContext
.
public RequestDispatcher getRequestDispatcher(String path)
The RequestDispatcher
is created based on the path used
to obtain it (must begin with a "/"). This is a context relative resource.
public void forward(ServletRequest request, ServletResponse response) public void include(ServletRequest request, ServletResponse response)
Forwarding is usually used when one servlet (or JSP) processes the request, and the other processes the response. Include is a resource for doing server side includes (allowing other servlets to do part of the request handling or response generation
We need to understand Request Dispatching in order to integrate Servlets and JSPs properly.
JSP/Servlet Differences [3/12] |
Servlets
JSPs
Note that you can run all the JSP steps manually outside of the servlet container if you wish. This can be helpful when doing debugging of some JSP problems, but most likely you'll want to just use the Tomcat defaults which are set up for development.
Webapp components [4/12] |
Primary components
The interaction between these three will allow the JSP and Servlet to communicate efficiently and in a maintainable way
More on JavaBeans later
Implicit Objects and their Scope [5/12] |
Objects can be manipulated in JSP.
Objects exist in different scopes:
Remember, and object created inside a method would only have scope in that
method as well. In Java, we also limit scope by using the { }
An object with a given scope is accessible to pages that are in the same scope as the page where the object was created. (more on this later)
Certain objects are created implicitly for each page by the container:
request
response
pageContext
session
application
out
config
page
exception - for error pages only
request
javax.servlet.ServletRequest
(i.e.
javax.servlet.http.HttpServletRequest
)
response
javax.servlet.ServletResponse
(i.e. javax.servlet.http.HttpServletResponse
)
pageContext
getOut(), getException(), getPage() getRequest(),
getResponse(), getSession(), getServletConfig()
and
getServletContext().
setAttribute(), getAttribute(), findAttribute(),
removeAttribute(), getAttributesScope()
and
getAttributeNamesInScope().
forward(), include(),
and
handlePageException().
session
javax.servlet.http.HttpSession
)application
getServletConfig().getContext()
in a servletout
javax.servlet.jsp.JspWriter
config
javax.servlet.ServletConfig
that you would get
from calling getServletConfig()
in a servletpage
java.lang.Object
exception
java.lang.Throwable
JSP Directives [6/12] |
Directives are page dependent properties that are passed on to the JSP container
3 Types
Syntax:<%@ directive { attr="value" }* %>
Page
Syntax:<%@ page page_directive_attr_list %>
page_directive_attr_list ::= { language="java"} { extends="className" } - JSP base class { import="importList" } -java.lang.*, javax.servlet.*, javax.servlet.jsp.* and javax.servlet.http.* { session="true|false" } - whether the page participates in a session { buffer="none| 8kb |sizekb" } { autoFlush="true|false" } -illegal to make buffer = none when autoflush is false { isThreadSafe="true|false" } { info="info_text" } { errorPage="error_url" } { isErrorPage="true|false" } { contentType="ctinfo" } - "text/html" { pageEncoding="peinfo" } - "ISO-8859-1"
Taglib
Syntax:<%@ taglib uri="tagLibraryURI" prefix="tagPrefix" %>
Example:
<%@ taglib uri="http://www.mycompany.com/supertags" prefix="myco" /> ... <myco:TagName> ... </myco:TagName>
Include
Syntax:<%@ include file="relativeURLspec" %>
Example:
<%@ include file="copyright.html" %>
Requests the inclusion of the file at translation time
JSP Actions [7/12] |
Special tags that affect the out stream and use, modify, or create objects
Standard Actions
<jsp:forward> <jsp:include> <jsp:param> <jsp:plugin> <jsp:params> <jsp:fallback> <jsp:useBean> <jsp:getProperty> <jsp:setProperty> <jsp:attribute> <jsp:element> <jsp:body> <jsp:invoke> <jsp:doBody> <jsp:text> <jsp:output>
The last 7 will be discussed once we learn about custom tag libraries
Custom actions: created with tag libraries
forward
Syntax : <jsp:forward page="relativeURLspec" />
<% String dest = "/location/"+someValue; %>
<jsp:forward page='<%= dest %>' />
include
Syntax:
<jsp:include page="urlSpec" flush="true|false"/>
and
<jsp:include page="urlSpec" flush="true|false"> { <jsp:param .... /> }* </jsp:include>
Include a resource at request time, or a request with parameters
param
Used with include and foward and params elements
Syntax:
<jsp:param name="name" value="value" />
plugin
replaces the tag with either an <object> or <embed> tag as needed by the user agent to display an applet or similar plugin properly. The params and fallback actions are used to provide for passing parameters to the plugin and providing feedback to the user if the plugin failed to load.
Example:
<jsp:plugin type=applet code="Molecule.class" codebase="/html" > <jsp:params> <jsp:param name="molecule" value="molecules/benzene.mol"/> </jsp:params> <jsp:fallback> <p> unable to start plugin </p> </jsp:fallback> </jsp:plugin>
JavaBeans [8/12] |
Definition (from the spec):
"A Java Bean is a reusable software component that can be
manipulated visually in a builder tool."
For this class, used to encapsulate Business Logic, as well as data for the presentation layer
Beans are just Java classes that follow various conventions and design patterns and can make use of the java.beans.* classes and interfaces
Learn more about JavaBeans at http://java.sun.com/products/javabeans/
Beans will vary in functionality, but can support:
JavaBean basics [9/12] |
Need a zero argument constructor
All fields should be private with Accessors for public access to variables
Type value; public Type getValue() { return value; } public void setValue(Type value) { this.value = value; }
boolean value; public boolean isValue() { return value; } public void setValue(boolean value) { this.value = value; }
Type[] value; public Type[] getValue() { return value; } public void setValue(Type value[]) { this.value = value; } public Type getValue(int i) { return value[i]; } public void setValue(int i, Type v) { value[i] = v; }
Property names begin with lower case
Property names are capitilized in the getter/setter methods
Acronyms are in all CAPS
Examples:
foo getFoo(), setFoo()
fooBar getFooBar(), setFooBar()
URL getURL(), setURL()
These conventions will have to be followed to use Beans in JSPs properly
Using JavaBeans in JSPs [10/12] |
Declaring the Bean
<jsp:useBean> action
Locates or instantiates a bean with a specified name and scope.
Syntax:
<jsp:useBean id="name" scope="page|request|session|application" typeSpec /> typeSpec ::= class="className" | class="className" type="typeName" | type="typeName" class="className" | beanName="beanName" type="typeName" | type="typeName" beanName="beanName" | type="typeName"
class must be assignable to type if both are declared
If the useBean has a body, it is of the form:
<jsp:useBean id="name" scope="page|request|session|application" typeSpec > body </jsp:useBean>
The body will be invoked if the Bean is created.
Typically, the body will contain either scriptlets or jsp:setProperty tags that will modify the object created in the useBean tag
The contents of the body are not restricted.
Semantics:
<jsp:useBean>
element
has a non-empty body, the body is processed.
Using JavaBeans in JSPs, manipulating data [11/12] |
You can also manipulate JavaBeans using the get/setProperty actions
getProperty
Example:
<jsp:getProperty name="user" property="name" />
Syntax:
<jsp:getProperty name="name" property="propertyName" />
Value is obtained using toString() method
setProperty
Bean referred to in name must be defined in a useBean tag
Example:
Setting from request parameters
<jsp:setProperty name="request" property="*" /> <jsp:setProperty name="user" property="user" param="username" />
Setting from a value
<jsp:setProperty name="results" property="row" value="<%= i+1 %>" />
Syntax:
<jsp:setProperty name="beanName" prop_expr /> prop_expr ::= property="*" | property="propertyName"| property="propertyName" param="parameterName"| property="propertyName" value="propertyValue"
propertyValue ::= string
Homework [12/12] |
Goal: To use a RequestDispatcher, a JavaBean, and JSP/Servlet integration.
Supply the application as a deployable war file. Test deploying it before submitting it to make sure you have the format right. Place the source files for the servlet in a src directory in the war file. Please link the homework assignment to the index.html file in the document root.
Grading Breakdown:
Item | Points |
---|---|
configuration (web.xml) | 10 |
input form | 10 |
JSP that uses the JavaBean | 10 |
Proper JavaBean implementation | 10 |
Proper use of RequestHandler | 10 |
concept and implementation | 30 |
Coding style and JavaDoc | 10 |
Proper Submission | 10 |
Total: | 100 |
Revised: 10/11/2003