Title: Object-Oriented Enterprise Application Development
1Object-Oriented Enterprise Application Development
2Topics
- During this class we will examine
- Anatomy of a JavaServer Page
- Merging markup with Java
- Configuring our JSPs with directives
- Integrating servlets and JSPs
3JavaServer Pages
4Justification
- JavaServer Pages (JSPs) are a technology that
allow developers to mix static with dynamic
content. - JavaServer Pages have been successful for two (2)
reasons - JSPs are supported across multiple platforms.
- JSPs give developers access to all Java
technologies instead of using a scripting
language.
5Roles and Responsibilities
- Although servlets can generate dynamic content,
it's often advantageous to separate the business
logic in the servlet from the presentation logic. - By using JSPs we can have application programmers
building servlets while content designers and
graphic artists construct the JSPs.
6Versions
- We'll use version 1.1 of the JavaServer Pages
specification. - The 1.0 and 1.1 version JSPs are almost totally
incompatible with the 0.92 version of the
specification.
7JSP Structure
8Basic Concepts
- At their heart, basic JSPs look just like regular
HTML markup. - A static HTML document can be turned into a JSP
simply by changing its extension to .jsp. - The key difference is that JSPs are dynamic.
- When a JSP is executed, the JSP engine converts
that JSP into an equivalent servlet which then
follows the normal lifecycle.
9Sample Code - HelloWorld
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.0
Transitional//EN"gt - lt-- comments look like this --gt
- ltHTMLgt
- ltHEADgt
- ltTITLEgtHelloWorld JSPlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltH2gtHello, World!lt/H2gt
- lt/BODYgt
- lt/HTMLgt
10Dynamic Content
- If all we needed was static content, then we'd
just use HTML. The strength of JSPs lie in their
ability to generate and manipulate dynamic
content. - There are three common ways of performing this
generation using JSPs - Expressions,
- Scriptlets
- Declarations
11Lifecycle
- Even though JSPs are ultimately compiled into
servlets, there are a few differences in their
lifecycle. - Most of the tags for generating dynamic content
are placed within the JSP's jservice() method. - This method is called by the JSP's service()
method for both GET and POST requests.
12JSP Tags
13Basic Tags
- In the 0.92 version of the JSP specification, we
identified dynamic content using special tags. - In version 1.1 of the JSP specification we can
still use some of these tags. We can also use
equivalent XML tags.
14JSP Expressions
- The simplest form of dynamic content is the JSP
expression. This is used to send a value back to
the client. - The tag format of an expression is given by
- lt java expression gt
15Sample Code - DynamicHelloWorld
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - ltHTMLgt
- ltHEADgtltTITLEgt
- DynamicHelloWorld JSP
- lt/TITLEgtlt/HEADgt
- ltBODYgt
- ltH2gtHello, World!lt/H2gtltBRgt
- ltH3gtIts now
- lt new java.util.Date() gt
- lt/H3gt
- lt/BODYgt
- lt/HTMLgt
16Implicit Variables(1 of 3)
- There are eight (8) implicit variables for each
JSP. The four (4) most common are - request The HttpServletRequest object passed to
the JSP. - response The HttpServletResponse object passed
to the JSP. - session The client's HttpSession object.
- out The PrintWriter object used to send output
to the client that initiated the request.
17Implicit Variables(2 of 3)
- Other useful variables include
- application The ServletContext object.
- config The ServletConfig object.
- pageContect The PageContext object.
- page The this object (not currently used).
18Implicit Variables(3 of 3)
- These implicit variable can be used within JSP
expressions and scriptlets, but not within JSP
declarations.
19Sample Code Status(1 of 2)
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - ltHTMLgt
- ltHEADgtltTITLEgt
- StatusHelloWorld JSP
- lt/TITLEgtlt/HEADgt
- ltBODYgt
- ltH2gtHello, World!lt/H2gtltBRgt
- ltH3gtCurrent Time
- lt new java.util.Date() gt
- lt/H3gtltBRgt
- ltH3gtSession ID
- lt session.getId() gt
- lt/H3gtltBRgt
20Sample Code Status(2 of 2)
- ltH3gtHostname
- lt request.getRemoteHost() gt
- lt/H3gtltBRgt
- ltH3gtParameter
- lt request.getParameter("test") gt
- lt/H3gt
- lt/BODYgt
- lt/HTMLgt
21JSP Scriptlets
- Often a single expression isn't adequate for the
complexity of the output we want the JSP to
generate. - JSPs allow us to embed complete segments of Java
code within a scriptlet. - The tag format of a scriptlet is given by
- lt java code gt
22Sample Code Scriptlet(1 of 2)
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - ltHTMLgt
- ltHEADgt
- ltTITLEgtScriptlet JSPlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltH2gtHello, World!lt/H2gtltBRgt
- ltH3gtCurrent Time
- lt new java.util.Date() gt
- lt/H3gtltBRgt
- ltH3gtSession ID
- lt session.getId() gt
- lt/H3gtltBRgt
23Sample Code Scriptlet(2 of 2)
- ltH3gtHostname
- lt request.getRemoteHost() gt
- lt/H3gtltBRgt
- lt
- String testString request.getParameter
("test") - if (null testString)
- testString "no.such.parameter"
-
- gt
- ltH3gtParameter lt testString gtlt/H3gt
- lt/BODYgt
- lt/HTMLgt
24Conditional Tags
- JSP scriptlets are useful for including
conditional content. - This cuts down on the amount of data returned to
the client. - This technique can be used to implement basic
security features.
25Sample Code Scriptlet (rev.)(1 of 3)
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - ltHTMLgt
- ltHEADgt
- ltTITLEgtScriptlet JSPlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltH2gtHello, World!lt/H2gtltBRgt
- ltH3gtCurrent Time
- lt new java.util.Date() gt
- lt/H3gtltBRgt
26Sample Code Scriptlet (rev.)(2 of 3)
- lt
- if (session.getId() ! null)
- gt
- ltH3gtSession ID
- lt session.getId() gt
- lt/H3gtltBRgt
- lt
-
- gt
- ltH3gtHostname
- lt request.getRemoteHost() gt
- lt/H3gtltBRgt
27Sample Code Scriptlet (rev.)(3 of 3)
- lt
- String testString
request.getParameter("test") - if (null testString)
- testString "no.such.parameter"
-
- gt
- ltH3gtParameter
- lt testString gt
- lt/H3gt
- lt/BODYgt
- lt/HTMLgt
28Translated Code Scriptlet(1 of 8)
- import javax.servlet.
- import javax.servlet.http.
- import javax.servlet.jsp.
- import javax.servlet.jsp.tagext.
- import java.io.PrintWriter
- import java.io.IOException
- import java.io.FileInputStream
- import java.io.ObjectInputStream
- import java.util.Vector
- import org.apache.jasper.runtime.
- import java.beans.
- import org.apache.jasper.JasperException
29Translated Code Scriptlet(2 of 8)
- public class _0002fHtml_0002fscriptlet_0002ejspscr
iptlet_jsp_0 extends HttpJspBase - static
- public _0002fHtml_0002fscriptlet_0002ejspscriptlet
_jsp_0( ) - private static boolean _jspx_inited false
- public final void _jspx_init() throws
JasperException
30Translated Code Scriptlet(3 of 8)
- public void _jspService( HttpServletRequest
request, HttpServletResponse response) - throws IOException, ServletException
- JspFactory _jspxFactory null
- PageContext pageContext null
- HttpSession session null
- ServletContext application null
- ServletConfig config null
- JspWriter out null
- Object page this
- String _value null
31Translated Code Scriptlet(4 of 8)
- try
- if (_jspx_inited false)
- _jspx_init()
- _jspx_inited true
-
- _jspxFactory JspFactory.getDefaultFact
ory() - response.setContentType(
- "text/htmlcharset8859_1")
- pageContext _jspxFactory.getPageContex
t( this, request, response, "",
true, 8192, true)
32Translated Code Scriptlet(5 of 8)
- application pageContext.getServletCont
ext() - config pageContext.getServletConfig()
- session pageContext.getSession()
- out pageContext.getOut()
- out.write("lt!DOCTYPE HTML PUBLIC
\"-//W3C//DTD HTML 4.0 Transitional//EN\"gt\r\nltHT
MLgt\r\n ltHEADgt\r\n ltTITLEgtScriptlet
JSPlt/TITLEgt\r\n lt/HEADgt\r\n
ltBODYgt\r\n\t\t\tltH2gtHello, World!lt/H2gt\tltBRgt\r\n\t
\t\tltH3gtCurrent Time ")
33Translated Code Scriptlet(6 of 8)
- out.print( new java.util.Date() )
- out.write("lt/H3gt\r\n\t\t\t"-ltBRgt\r\n\t\t\t")
- if (session.getId() ! null)
- out.write("\r\n\t\t\t\tltH3gt"-Session ID
") - out.print( session.getId() )
- out.write("lt/H3gtltBRgt\r\n\t\t\t")
-
- out.write("\r\n\t\t\tltH3gt"-Hostname ")
- out.print( request.getRemoteHost() )
- out.write("lt/H3gtltBRgt\r\n\t\t\t")
34Translated Code Scriptlet(7 of 8)
- String testString request.getParameter
("test") - if (null testString)
- testString "no.such.parameter"
-
- out.write("\r\n\t\t\tltH3gt"-Parameter ")
- out.print( testString )
- out.write("lt/H3gt\r\n\tlt/BODYgt"-\r\nlt/HTMLgt")
35Translated Code Scriptlet(8 of 8)
- catch (Exception ex)
- if (out.getBufferSize() ! 0)
- out.clearBuffer()
- pageContext. handlePageException(ex)
- finally
- out.flush()
- _jspxFactory. releasePageContext(pageCon
text) -
-
36JSP Declarations
- A JSP declaration exists outside of the
jservice() method. - This means that code in a declaration cannot make
use of the implicit variables. - This allows us to declare JSP-level variables and
methods. - The tag format of an expression is given by
- lt! java declaration gt
37Sample Code Declaration(1 of 2)
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - ltHTMLgt
- ltHEADgt
- ltTITLEgtDeclaration JSPlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltH2gtDeclarationslt/H2gtltBRgt
- ltH3gtCurrent Time
- lt new java.util.Date() gt
- lt/H3gtltBRgt
- ltH3gtSession ID
- lt session.getId() gt
- lt/H3gtltBRgt
38Sample Code Declaration(2 of 2)
- ltH3gtHostname
- lt request.getRemoteHost() gt
- lt/H3gtltBRgt
- lt! private int accesses 0 gt
- ltH3gt
- lt accesses gt
- Accesses to the page since JSP was
loaded. - lt/H3gt
- lt/BODYgt
- lt/HTMLgt
39JSP Directives
40Overview
- We use directives to customize the JSP's
behavior. Each directive affects the structure of
the servlet resulting from the JSP's compilation. - There are three (3) types of directives
- page Controls the servlet's structure.
- include Inserts a file into the servlet class.
- taglib Defines custom markup tags.
41Page Directives
- There are many page directives. We'll look at a
few of the most common attributes - import
- isThreadSafe
- session
- errorPage
- We use the directive tag to indicate that we want
a directive to take effect - lt_at_ directive gt
42Import Attribute
- The import attribute allows a JSP to import class
libraries - lt_at_ page import"java.util." gt
- By default a JSP automatically imports
- java.lang.
- java.servlet.
- java.servlet.http.
- java.servlet.jsp.
43Sample Code - Import
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - lt_at_ page importjava.util.Date gt
- ltHTMLgt
- ltHEADgt
- ltTITLEgtImport JSPlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltH2gtImportslt/H2gtltBRgt
- ltH3gtCurrent Time
- lt new Date() gt
- lt/H3gt
- lt/BODYgt
- lt/HTMLgt
44isThreadSafe Attribute
- The isThreadSafe attribute forces the JSP's
servlet to implement the SingleThreadedModel
interface - lt_at_ page isThreadSafe"true" gt
- By default a JSP is assumed to be thread safe in
the same way that servlets are. - If this isn't the case you can
- Make it thread-safe using synchronized blocks
- Set isThreadSafe to false.
45Sample Code IsThreadSafe(1 of 2)
- lt! private int id 0 gt
- lt
- String userId "id" id
- out.println("Your id is " id)
- id
- gt
46Sample Code IsThreadSafe(2 of 2)
- lt! private int id 0 gt
- lt
- synchronized (this)
- String userId "id" id
- out.println("Your id is " id)
- id
-
- gt
47Sample Code - IsThreadSafe
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - lt_at_ page importjava.util.Date gt
- lt_at_ page isThreadSafefalse gt
- ltHTMLgt
- ltHEADgt
- ltTITLEgtSingle Thread JSPlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- lt! private int accesses 0 gt
- lt accesses gt
- Accesses since JSP was loaded.
- lt/BODYgt
- lt/HTMLgt
48Session Attribute
- The session attribute controls whether or not the
JSP can access the client's session - lt_at_ page session"true" gt
- By default a JSP participates in the client's
session. - lt_at_ page session"true" gt
- If this isn't the behavior we want, we can turn
it off - lt_at_ page session"false" gt
49errorPage Attribute
- The errorPage attribute specifies the URL to
which the client will be sent if an unhandled
exception is encountered - lt_at_ page errorPage"some URL" gt
- The exception that caused the problem will be
provided to the page via the exception variable.
50Sample Code - ErrorPage
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - lt_at_ page errorPageerror.jsp gt
- ltHTMLgt
- ltHEADgt
- ltTITLEgtErrorPage JSPlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltH3gtSession Variable
- lt
- session.
- getAttribute("var").toString()
- gt
- lt/BODYgt
- lt/HTMLgt
51Integrating Servlets and JavaServer Pages
52Integration
- JavaServer Pages allow graphic designers to
achieve reasonable separation between their roles
and the role of the servlet developer. - However, since all of these components will be
used together in the final application, we need
to integrate them.
53Request Dispatcher
- We've already looked at one approach to
integrating servlets and JSPs the
RequestDispatcher class. - This remains the simplest way of invoking a JSP
from a servlet.
54Shared Data
- It isn't uncommon to need to share data between a
servlet and a JSP. - The servlet may perform all of the business
processing necessary to generate the data to be
displayed by the JSP. - We could use the client's session, but what if
the data isn't required beyond a single
request-response pair?
55Request Attributes(1 of 2)
- For data that needs to exist only for a single
client request, we can use the HttpServletRequest
object to store our data. - In addition to the parameters passed in by the
client, the HttpServletRequest contains
attributes which can be set by servlets and JSPs.
56Request Attributes(2 of 2)
- As with session attributes, request attributes
are nothing more than a name-value pair. - To manipulate these attributes you can use the
following methods - public void
- setAttribute(String, Object)
- public Object
- getAttribute(String)
57Sample Code SetData(1 of 3)
- import java.io.
- import java.util.
- import javax.servlet.
- import javax.servlet.http.
- public class SendData
- extends HttpServlet
- public void doPost(HttpServletRequest rqst,
HttpServletResponse resp) - throws ServletException, IOException
- doGet(rqst, resp)
-
58Sample Code SetData(2 of 3)
- public void doGet(HttpServletRequest rqst,
HttpServletResponse resp) - throws ServletException, IOException
- rqst.setAttribute("parm", "skippy")
- chain(rqst, resp, "GetData.jsp")
-
59Sample Code SetData(3 of 3)
- private void chain(HttpServletRequest rqst,
HttpServletResponse resp, String
URL) - throws ServletException, IOException
- RequestDispatcher dispatcher
getServletContext(). getRequestDispatcher(
URL) - dispatcher.forward(rqst, resp)
-
60Sample Code - GetData
- lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt - ltHTMLgt
- ltHEADgt
- ltTITLEgtGetData JSPlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltH2gtGet Shared Valuelt/H2gtltBRgt
- ltH3gtThe shared value is...
- lt request.getAttribute("parm") gt
- lt/H3gt
- lt/BODYgt
- lt/HTMLgt
61Review
- During this class we have discussed
- Anatomy of a JavaServer Page
- Merging markup with Java
- Configuring our JSPs with directives
- Integrating servlets and JSPs
62Resources
- Core Servlets and JavaServer PagesMarty Hall,
Prentice-Hall, Inc., 2000.ISBN 0-13-089340-4 - Java 2 Platform, Enterprise EditionB. Shannon,
et al., Addison-Wesley, 2000.ISBN 0-201-70456-0
63Coming Attractions
- Next week we'll add custom tags to our JavaServer
Pages. - Please read Chapter 14 in your text.