Introducing OOP
Download
Report
Transcript Introducing OOP
Application Integration
Servlets
Introduction & Overview
HTTP Servlets
HTTP get Requests
HTTP post Requests
Multi-tier Applications Using JDBC from a Servlet
References
Michael Brockway
Introduction
References: Y D Liang, Intro to Java Programming, 7ed chapter 39
Java’s support for distributed / internet- / web-based applications includes
Multithreading
Networking
Remote Method Calling
java.net package
Socket-based communications, packet-based communications
RMI (java.rmi package)
CORBA
Servlets
A client requests an action be performed; a server responds to client requests
An applet is a class that is downloaded to the client and performs functions there;
A servlet lives in the server and responds to requests from the client.
Basic packages are javax.servlet, javax.servlet.http
javax.servlet.jsp and javax.servlet.jsp.tagext comprise Java Server Pages (JSP),
extending servlet capabilities to enable servers to deliver to the client
dynamically created XHTML pages with embedded Java functionality.
2
Introduction
Client
Web Server
business logic
eg database
Servlets (ctd)
Back End
A distributed application often has a 3-tier architecture with a web server providing
secure access to a “back end”, commonly a database system.
Servlets may be used to support thin clients with minimal client-side software.
The client sends one of a small number of requests to the server; servlets residing in
the server respond
Clients connect to the server using standard protocols
HTTP forms
“get”, “post” requests
See http://www.w3.org/Addressing for information on URLs, and
http://www.w3.org/Protocols/HTTP
This chapter shows how servlets manage client/server communication using
the HTTP protocol
Client sends an HTTP request
Servlet processes it and returns a response to client
3
Servlet Architecture
A servlet is a class which implements the javax.servlet.Servlet interface
The packages provide two abstract classes implementing this
void init(ServletConfig cfg)
parameter supplied by server containing the servletclass
ServletConfig getServletConfig()
returns reference to implementing object
String getServletInfo()
void service(ServletRequest rq, ServletResponse rp)
Containing server calls this in response to a client request to the servlet.
void destroy()
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
Most servlets extend one of these. We shall concentrate on the HttpServlet from now on.
Class HttpServlet
Has empty methods , at leat one of which should be over-ridden:
doGet(HttpServletRequest rq, HttpServletResponse rp) and
doPost(HttpServletRequest rq, HttpServletResponse rp)
Overrides service() with a version which calls one of these, depending on request type;
An HttpServlet needs to define specific overriding doGet() and/or doPost() methods
4
Servlet Architecture
Interface HttpServletRequest
String getParameter(String param)
cf applet parameter
Enumeration getParameterNames()
names of all parameters set as part of a post request
String[] getParameterValues(String paramName)
a parameter can have multiple values
Cookie[] getCookies()
cookies are stored on the client by the server (see later)
HttpSession getSession (boolean create)
returns an HttpSession associated with current browsing session (see later)
if create = true, creates one if one does not already exist
Interface HttpServletResponse
void addCookie(Cookie cookie)
added to header of response to client. Stored in client if client has enabled cookies.
ServletOutputStream getOutputStream()
PrintWriter getWriter()
byte-based (binary) and character-based output to client, respecively
cookies are stored on the client by the server (see later)
void setContentType(String type)
MIME type of output to client -- “text/html” for HTML content
Every call to doGet(...), doPost(...) gets from containing server objects implementing these .
5
Handling get Requests
WelcomeServlet
Example
WelcomeServlet.java + WelcomeServlet.html (See listings at back)
Note the tag in the XHTML form in WelcomeServlet.html:
<form action = “/servletexs/welcome1” method = “get”>
directs a request from this form to the server, to the WelcomeServlet;
Establishes it as a get request
The tag
<input type = “submit” value = “Get HTML Document”/>
displays a button labelled “Get HTML Document”
The WelcomeServlet, once correctly deployed in the server, responds by
When the client loads this XHTML and clicks the button the get request is sent to the
server
Setting the response content type to “text/html”
Getting the output PrintWriter object for the response -- called out
Using out.println(...) to output lines of HTML to the client.
Practicalities
javax.servlet and javax.servlet.http are J2EE packages: J2EE needs to
be installed in the development environment and an appropriate CLASSPATH set.
6
Handling get Requests
Deploying the WelcomeServlet in Tomcat Server
to serve the get request from this XHTML form
Look at Tomcat’s webapps directory
In here make a directory servletexs
the context root of this Web application
In this directory install the following subdirectories and files (files in italics):
servletexs
WelcomeServlet.html
WEB-INF
web.xml
classes
WelcomeServlet.class
web.xml is the deployment descriptor also listed at the back of these slides
Ties the servlet to incoming get requests from client forms: see Servlet Mappings section
Client runs the servlet by first loading the form generating the get request:
http://localhost:8080/servletexs/WelcomeServlet.html
7
Handling get Requests Containing Data
WelcomeServlet2
WelcomeServlet2.java + WelcomeServlet2.html ( See listings at back)
Note the tag in a XHTML form in WelcomeServlet2.html as before:
<form action = “/servletexs/welcome2” method = “get”>
The tags within the form
<input type = “text” name = “firstname”/>
<input type = “submit” value = “Submit”/>
display a text box and a “Submit” button.
When the client clicks it the get request is sent to the server
with parameter named “firstname” with value = contents of text box
WelcomeServlet2 can use request.getParameter(“firstname”) to obtain the
data entered by the client in the form.
Deploying:
Install WelcomeServlet2.java, WelcomeServlet2.html in Tomcat in advjhtp1/...
Edit web.xml to include <servlet> and <servlet-mapping> elements for this servlet
Restart Tomcat
Client
http://localhost:8080/servletexs/WelcomeServlet2.html
8
Handling post Requests
WelcomeServlet3
WelcomeServlet3.java + WelcomeServlet3.html ( See listings at back)
Note the tag in a XHTML form in WelcomeServlet3.html:
<form action = “/servletexs/welcome3” method = “post”>
The tags within the form
<input type = “text” name = “firstname”/>
<input type = “submit” value = “Submit”/>
display a text box and a “Submit” button.
When the client clicks it the post request is sent to the server
with parameter named “firstname” with value = contents of text box
WelcomeServlet3 uses request.getParameter(“firstname”) to obtain the
data entered by the client in the form.
Deploying:
Install WelcomeServlet3.java, WelcomeServlet3.html in Tomcat in advjhtp1/...
Edit web.xml to include <servlet> and <servlet-mapping> elements for this servlet
Restart Tomcat
Client
http://localhost:8080/servletexs/WelcomeServlet3.html
9
Multi-Tier Applications -- Using JDBC from a Servlet
Client
XHTML
Web Server
JDBC
middle tier
db access
SurveyServlet.java, Survey.html
Functionality
Back End
Collects a vote from each client “What is your favourite pet?”
Stores votes in database
Returns to client a report of survey statistics
Thin client: just a web browser
Middle tier: the SurveyServlet
Back end: a Cloudscape database connected to middle tier by JDBC
ServeyServlet
init(...)
Initially sets up a JDBC Connection to the database, and SQL PreparedStatement
objects to update the database and extract statistics from it
10
Multi-Tier Applications -- Using JDBC from a Servlet
ServeyServlet
doPost(...)
Survey.html
Form with post method, containing
Radio buttons with name = “animal”for the client to vote
A Submit button
Deploy in the usual way
Gets vote from client
request.getParameter(“animal”)
Uses a Statement to update the database
Uses Statement objects to get get statistics
Sends XHTML response to client
Install class, html file, and add animalsurvey elements to web.xml; restart Tomcat.
servlet-name = animalsurvey, url-pattern = /animalsurvey, etc
Also copy ojdbc14.jar (containing the Oracle JDBC driver) to Tomcat’s
...\servletexs\WEB-INF\lib subdirectory.
11
Session Tracking -- Cookies
A way for a server to keep persistent information about a particular client
Text based data
If cookies enabled in client’s browser,
preferences
contents of an e-shopping cart
etc
Server can send cookies in header of an XHTML page
Client browser stores them in client machine
Sent back to server with next get or post from client
Cookies expire after a finite amount of time
See Liang
CookieServlet.java, CookieSelectLanguage.html
12
Session Tracking -- HttpSession Interface
Another way of supporting a server to maintain information about client
preferences
See , eg Liang
SessionServlet.java, SessionSelectLanguage.html
The Request object provides an object implementing HttpSession
Same functionality as Cookie example
Unlike Cookies, HttpSession objects are not normally saved between browser
sessions but just “live” for the duraiton of a session.
13
Further Reading
WWW Resources
The Sun java enterprise edition web site has plenty on servlets.
Go to http://java.sun.com/ ffollow the links to API documentation on the
Classes and interfaces in the javax.servlet and javax.servlet.http
packages, and tutorial and background reading on servlets.
Explore http://jakata.apache.org/tomcat/ For reading about servlets
deployed in Tomcat
The Liang textbook references cited above.
The O’Reilly book Java Servlet Programming
14