Introducing OOP - Northumbria University
Download
Report
Transcript Introducing OOP - Northumbria University
Application Integration
JavaServer Pages
Introduction & Overview
Implicit Objects
Scripting
Standard Actions
Directives
References
Michael Brockway
Introduction
References: Deitel & Deitel Java chap 25; Liang, Java Programming, chap
40
JavaServer Pages (JSPs) are a way of simplifying the delivery of dynamic
web content
Underneath, they are servlets, but you do not program them in Java;
Rather, you write XHTML documents with embedded
directives
actions
Scriptlets (fragments of Java code)
tag libraries
Accessible to non Java programmers
Even experienced Java programmers are prone to errors when writing a lots of
printlln(...) statements to a reponse object’s printWriter, in order to
create a dynamic web page: it is easier to write the web page and embed the
dynamic bits.
2
Introduction
When a JSP-enabled server receives a request for a JSP
The JSP container (within the server) translates the JSP into a Java servlet
The servlet handles the current and future requests for this JSP
Errors compiling the servlet result in translation-time errors.
JSP container places the Java statements that implement the JSP’s response in
method _jspService at translation time.
If the JSP compiles correctly, the JSP container invokes method _jspService
to process the request.
May respond directly; or
May invoke other Web application components
A first example – clock.jsp
Deploy in Tomcat
...webapps\jspexs\jsp\clock.jsp
jspexs is the context root for the present jsp examples
In web browser, go to
http://localhost:8080/jspexs/jsp/clock.jsp
3
Implicit Objects
A JSP page is XHTML with embedded pieces of Java. These java statements
can refer to a number of implicit objects associated with the page, or the
HTTP request which led to the production of the page, or with the browser
session.
javax.servlet.ServletContext application
javax.servlet.ServletConfig config
Represents the JSP configuration options
Has page scope: exists just in the current page
java.lang.Throwable exception
This object represents the container in which the JSP executes
Scope is “global” – any servlet or JSP in this container can access it
Passed to a JSP error page; has page scope.
javax.servlet.jsp.JspWriter out (page scope)
Writes text in response to a request
Used implicitly with JSP expressions and actions that output string content
java.lang.Object page (page scope)
The this reference
4
Implicit Objects
Implicit objects (ctd)
javax.servlet.jsp.PageContext pageContext (page scope)
Hides implementation of page’s underlying servlet
Provides access to these implicit objects
javax.servlet.http.HttpServletResponse response (page
scope)
Or some class that implements javax.servlet.ServletResponse
Implements response to client
javax.servlet.http.HttpServletRequest request
Or some class that implements javax.servlet.ServletRequest
Has request scope: accessible to all pages which are part of this request
Represents the client request
javax.servlet.http.HttpSession session
Represents client session information
Has session scope – the clients entire brwosing session
5
Scripting
You write XHTML with embedded JSP scripting components
Scriptlets
3 kinds of comments
blocks of Java code delimited by <% and %>.
Java expressions delimited by <%= and %>.
Placed in method method _jspService a translation time.
XHTML comments go between <!-- and --!> and can go anywhere in a JSP except
inside scriptlets
JSP comments go between <%-- and --%> and can go anywhere in a JSP except inside
scriptlets; do not appear in response to client
Java comments between /* and */ or between // and end-of-line can go inside
scriptlets
Escape sequences
If you want a literal <%, use escape sequence <\%; for literal %>, use %\>
For literal ', ", \ use escape sequences \', \", \\
6
Scripting
See scripting example welcome.jsp
Deploy in Tomcat
http://localhost:8080/jspexs/jsp/welcome.jsp
…\webapps\jspexs\jsp\welcome.jsp
or
http://localhost:8080/jspexs/jsp/welcome.jsp?firstName=Michael
7
Standard Actions
See examples listed at end of these notes
<jsp:include>
<jsp:forward>
Specifies that the page uses a given JavaBean instance:
Specifies scope of bean
Assigns it an ID that scripting components can refer to
<jsp:setProperty>
Use with the 3 actions above to specify parameters as name,value pairs
<jsp:useBean>
Adds a browser-specific object to a page
<jsp:param
Forwards request to another JSP or servlet or static page.
Terminates current processing
<jsp:plugin>
Dynamically include another referenced resource in a JSP
in a given JavaBean instance
<jsp:getProperty>
in a given JavaBean instance
Converts to String for output to response
8
Directives
Messages to the JSP container allowing programmer to specifiy page settings
Delimited by <%@ and %>
Processed at translation time
include Directive
See example includeDirective.jsp
Guest Book case study
GuestBean.java
GuestDataBean.java
guestBookLogin.jsp
guestBookView.jsp
guestBookErrorPage.jsp
9
Further Reading
WWW Resources
Look at the links from http://java.sun.com/ Java Enterprise Edition
documentation
JavaServer Pages specification
http://java.sun.com/products/jsp/download.html#specs
Tutorial
http://java.sun.com/javaee/5/docs/tutorial//doc/
The API referece
http://java.sun.com/javaee/5/docs/api/
Look up packages javax.servlet.jsp and javax.servlet.jsp.tagext
The Deitel et al and Liang textbook references cited above.
10