Transcript Servlets

Servlets
Representation and Management
of Data on the Web
1
What is a Servlet?
• Servlets are Java programs that can be
run dynamically from a Web Server
• Servlets are a server-side technology
• A servlet is an intermediating layer
between an HTTP request of a client and
the Web server
2
What do Servlets do?
• Read data sent by the user (e.g., form data)
• Look up other information about request in the
HTTP request (e.g., headers, cookies, etc.)
• Generate the results (may do this by talking to
a database, file system, etc.)
• Format the results in a document (e.g., make it
into HTML
• Set the appropriate HTTP response parameters
(e.g., cookies, content-type, etc.)
• Send the document to the user
3
Why use a Servlet?
• Servlets are used to create dynamic
pages. Why?
– Page is based on data submitted by the user.
Example Scenario:
– Page is derived from data that changes often.
Example Scenario:
– Page uses information from server-side
sources. Example Scenario:
4
A Java Servlet
request
request
response
response
Servlet
Web
browser
Web
server
5
Supporting Servlets
• The Web server must support servlets (since it
must run the servlets):
– Apache Tomcat
– Sun’s JavaServer Web Development Kit (JSWDK)
– Allaire Jrun – an engine that can be added to IIS,
PWS, old Apache Web servers etc…
– Sun’s Java Web Server
– …
We will talk about
installing, configuring
and running tomcat
next week!
6
Compiling
• In order to compile a servlet, you may need to
add to your CLASSPATH definition the following:
setenv CLASSPATH ${CLASSPATH}:
/usr/local/java/apache/jakarta-tomcat/lib/ant.jar:
/usr/local/java/apache/jakarta-tomcat/lib/jasper.jar:
/usr/local/java/apache/jakarta-tomcat/lib/jaxp.jar:
/usr/local/java/apache/jakarta-tomcat/lib/parser.jar:
/usr/local/java/apache/jakarta-tomcat/lib/servlet.jar:
/usr/local/java/apache/jakarta-tomcat/
lib/webserver.jar
7
Servlet Package
• Need to import: javax.servlet.*;
• The Servlet interface defines methods that
manage servlets and their communication with
clients
• When a connection is formed, the servlet
receives two objects that implement:
– ServletRequest
– ServletResponse
8
Object Hierarchy
Servlet
Generic Servlet
HttpServlet
YourOwnServlet
9
Creating a Servlet
• Extend the class HTTPServlet
• Implement doGet or doPost (or both)
• Both methods get:
– ServletRequest: methods for getting form (query)
data, HTTP request headers, etc.
– ServletResponse: methods for setting HTTP status
codes, HTTP response headers, and get a PrintWriter
used for sending data to the client
• Usually implement doPost by calling doGet, or
vice-versa
10
import java.io.*;
import javax.servlet.*; import javax.servlet.http.*;
public class TextHelloWorld extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("Hello World");
out.close();
}
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
11
Returning HTML
• By default a text response is generated.
• In order to generate HTML
– Tell the browser you are sending HTML, by
setting the Content-Type header
– Modify the println to create a legal HTML
page
• You must set all headers before returning
the document content. Can you guess why?
12
import java.io.*;
import javax.servlet.*; import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>Hello
World</TITLE></HEAD>");
out.println(“<BODY><BIG>Hello World
</BIG></BODY></HTML>");
out.close();
}
}
13
14
Servlet Life Cycle
• No main() method!
• The server initializes and loads the servlet
• The servlet handles client requests
• The server can remove the servlet
• The servlet can remain loaded to handle
additional requests
• Incur startup costs only once (hopefully)
15
Servlet Life Cycle
Load into Web
server
Deal with requests:
call the
service method
Destroy the servlet:
call the
destroy method
Calling the
init method
ServletConfig
Servlet Class
Garbage
Collection
16
Initializing Servlets
• Performed when the servlet is loaded into the
Web server
• Servlet’s init(ServletConfig) method is called
• ServletConfig has methods to get initialization
parameters. You put these parameters in a file.
We will explain how next week.
• What would you do in the init method?
– creating tables in a database (yes / no)
– create Connection object to communicate with the
database (yes / no)
17
A Glance at Servlet
Configuration Parameters
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>InitExample</servlet-name>
<servlet-class>ServletInit</servlet-class>
<init-param>
<param-name>login</param-name>
<param-value>snoopy</param-value>
</init-param>
</servlet>
</web-app>
18
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.sql.*;
public class ServletInit extends HttpServlet {
Connection con;
public void init(ServletConfig config)
throws ServletException {
// Always call super.init
super.init(config);
String login = config.getInitParameter("login");
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:" +
login + "/" + login + "@sol4:1521:stud");
}
19
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><BODY><h1>Member List</h1><ul>");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select name from member");
while (rs.next())
out.println("<li>" + rs.getString(1));
out.println("</ul></BODY></HTML>");
}
}
20
The service Method
• Every time the server receives a request for
the servlet it:
– creates a thread for the request
– calls the service method
• The service method checks the type of request
(GET, POST, PUT, DELETE, TRACE, OPTION)
and call the appropriate method: doGet, doPost,
doPut, doDelete, doTrace, doOption
• It is recommended to implement doPost and
doGet instead of overriding service. Why?
21
HTTP Methods
• POST: Data sent in two steps
– Browser contacts server
– Sends data
• GET: Contacts server and sends data in single
step
– Called when you enter a URL in the browser directly
– Can be called by a form: appends form data to action
URL, separated by question mark
22
Other Methods
• HEAD: Client sees only header of response
to determine size, etc…
• PUT: Place documents directly on server
• DELETE: Opposite of PUT
• TRACE: Debugging aid returns to client
contents of its request
• OPTIONS: what options available on server
23
More on Service
• There is an automatic support for TRACE
and OPTIONS by servlet so you do not
have to implement them
• There is no method doHead. Why?
24
The Standard Model
• Usually there is a single instance of a servlet
and a thread for each user request
• The doGet and doPost methods must synchronize
access to data structures and other resources!
thread 1
Instance of
your Servlet
thread 3
thread 2
25
The Single Thread Model
• If you need to prevent concurrent access
to a single instance of your thread use the
single thread model
public class YourServlet extends HttpServlet
implements SingleThreadModel {
…
}
26
SingleThreadModel
• SingleThreadModel is a marker interface
– No methods
– Tells servlet engines about lifecycle expectations
• Ensure that no two threads will execute
concurrently the service method of the same
instance of the servlet
27
Server implementation of the
Single Thread Model
• Option 1: There is a pool of instances of
the servlet. Requests for the servlet are
queued get an arbitrary instance, or are
queued, if necessary.
• Option 2: For each request, an instance of
the servlet is created.
28
The Two Options
thread
thread 4
1
Instance of
your Servlet
thread 2
Pool of
Instances
thread 3
Instance of
your Servlet
Queue
thread 3
thread 1
Instance of thread 2
your Servlet
Instance of
your Servlet
thread 3
Instance of thread 4
your Servlet
Instance of
your Servlet
Instance
Per Thread
29
SingleThreadModel
• SingleThreadModel let you break servlet
functionality into multiple methods
• Can rely on “instance state” being uncorrupted
by other requests
• Does this prevents the need to synchronize
access to all resources?
• Can’t rely on static members
• Can't rely on persistent instance state between
connections
– The same client making the same request, can get
different instances of your servlet
30
Destroying Servlets
• The server may remove a loaded servlet, Why?:
– asked to do so by administrator
– Servlet was idle a long time
• Before removing, calls the destroy() method
– can perform cleanup, e.g., close database connections
• Is it possible for the servlet to end without
destroy being called?
31
Example – Counting Threads
public ShutdownExample extends HttpServlet {
private int serviceCounter = 0;
...
//Access methods for serviceCounter
protected synchronized void enteringServiceMethod()
{
serviceCounter++;
}
protected synchronized void leavingServiceMethod()
{
serviceCounter--;
}
protected synchronized int numServices() {
return serviceCounter;
}
32
protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
enteringServiceMethod();
try {
super.service(req, resp);
} finally {
leavingServiceMethod();
}
}
33
Notifying a Shutdown
public ShutdownExample extends HttpServlet {
private boolean shuttingDown;
//Number of services, from before...
//Access methods for shuttingDown
protected setShuttingDown(boolean flag) {
shuttingDown = flag;
}
protected boolean isShuttingDown() {
return shuttingDown;
}
}
34
public void destroy() {
/* Check to see whether there are still
* service methods running,
* and if there are, tell them to stop.
*/
if (numServices() > 0) {
setShuttingDown(true);
}
/* Wait for the service methods to stop. */
while(numServices() > 0) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {}
}
}
35
public void doPost(...) {
...
for(i = 0; ((i < numberOfThingsToDo)
&& !isShuttingDown()); i++) {
try {
partOfLongRunningOperation(i);
} catch (InterruptedException e) {}
}
}
36
Getting Information
From the User
37
Handling a User Request
• The servlet can get parameters from an
HTML form
• <form action=… method=…> …</form>
comprises a single form
– action – the address to which the form data is
sent
– method – the HTTP method to use when passing
parameters to the server
38
The <input> Tag
• Inside a form, INPUT tags define fields for
data entry
• Standard input types include: buttons,
checkboxes, password field, radio buttons, text
fields, image-buttons, text areas, hidden fields,
etc.
• They all associate a single (string) value with a
named parameter
39
Example
<HTML>
…
<form method=“GET”
action=“http://pita.cs.huji.ac.il:8090/servlet/upd
ate”>
…
<INPUT name=“x” type=“text”>
<INPUT name=“y” type=“text”>
<INPUT type=“submit”> <INPUT type=“reset”>
</form>
…
</HTML>
http://pita.cs.huji.ac.il:8090/servlet/update?x=19&y=104
40
Getting the Parameters Values
• To get the value of a parameter named x:
– req.getParameter("x")
where req is HTTPServletRequest object
• If there can be multiple values for the
parameter:
– req.getParameterValues()
• To get parameter names:
– req.getParameterNames()
41
<HTML>
<HEAD>
<TITLE>Sending Parameters</TITLE>
</HEAD>
<BODY BGCOLOR="#CC90E0">
<H1 ALIGN="LEFT">Please enter the parameters</H1>
<FORM ACTION=“dbi/servlet/SetColors” METHOD=“GET”>
<TABLE>
<TR><TD>Background color:</TD>
<TD><INPUT TYPE="TEXT" NAME="bgcolor"></TD></TR>
<TR><TD>Font color:</TD>
<TD><INPUT TYPE="TEXT" NAME="fgcolor"></TD></TR>
<TR><TD>Font size:</TD>
<TD><INPUT TYPE="TEXT" NAME="size"></TD></TR>
</TABLE>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Show Page">
</FORM>
</BODY>
</HTML>
42
http://pita.cs.huji.ac.il:8080/colors.html
43
http://pita.cs.huji.ac.il:8080/dbi/servlet/
SetColors?bgcolor=wheat&fgcolor=blue&size=5
44
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SetColors extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String bg = request.getParameter("bgcolor");
String fg = request.getParameter("fgcolor");
String size = request.getParameter("size");
45
out.println("<HTML><HEAD><TITLE>Set Colors Example" +
"</TITLE></HEAD>");
out.println("<BODY text='" + fg +
"' bgcolor='" + bg + "'>");
out.println("<H1>Set Colors Example</H1>");
out.println("<FONT size='" + size + "'>");
out.println("You requested a background color " +
bg + "<P>");
out.println("You requested a font color " +
fg + "<P>");
out.println("You requested a font size " +
size + "<P>");
out.println("</FONT></BODY></HTML>");
}
}
46
Think About it
• Could you have implemented this example
(or something similar) using Javascript?
• Should you use Javascript for something
like this? When?
47
Handling Post
• You don't have to do anything different
to read POST data instead of GET data!!
<FORM ACTION=“dbi/servlet/SetColors” METHOD=“POST”>
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
}
doGet(request, response);
48
Getting Information from
Client Request
• Suppose that req is a HTTPServletRequest
object
• Get the value of the header "Accept":
req.getHeader("Accept")
• Get all header names req.getHeaderNames()
• Methods for special types of headers and other
info: getCookies, getContentLength,
getContentType, getMethod, getProtocol, etc.
49
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
public class ShowRequestHeaders extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Servlet Example:
Showing Request Headers";
50
out.println("<HTML><HEAD><TITLE>" + title +
"</TITLE></HEAD>" +
"<BODY BGCOLOR=\"#AACCAA\" TEXT=\"#990000\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<B>Request Method: </B>" +
request.getMethod() + "<BR>\n" +
"<B>Request URI: </B>" +
request.getRequestURI() + "<BR>\n" +
"<B>Request Protocol: </B>" +
request.getProtocol() + "<BR><BR>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#88AA88\">\n" +
"<TH>Header Name<TH>Header Value");
51
Enumeration headerNames = request.getHeaderNames();
}
while(headerNames.hasMoreElements()) {
String headerName =(String)headerNames.nextElement();
out.println("<TR><TD>" + headerName);
out.println("<TD>“ + request.getHeader(headerName));
}
out.println("</TABLE>\n</BODY></HTML>");
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
52
53
54
Creating the
Response of the Servlet
55
HTTP Response
• The response includes:
– Status line: version, status code, status
message
– Response headers
– Empty line
– Document
HTTP/1.1 200 OK
Content-Type: text/plain
Bla Bla Bla
56
Response is Buffered
Servlet
server
Buffer
response
setBufferSize
getBufferSize
flushBuffer
etc.
request
client
57
Setting the Response Status
• The status code of the HTTP response can be
set by the setStatus method of
HTTPServletResponse
• The status should be defined before sending
content through the PrintWriter of the
HTTPServletResponse
• The status can be defined after setting headers
of the HTTPServletResponse
• The default status is 200
58
Shortcuts
• An automatic response is created by
– sendError(int code, String message)
• Returns the status code with the message
• Usually status code 404
(HTTPServletResponse.SC_NOT_FOUND)
– sendRedirect(String url)
• Creates a response with status code 302
(HTTPServletResponse.SC_MOVED_TEMPORARILY) with a
Location header that includes the given url
59
Example of Using Redirect
• Any ideas? (Remember ex2...)
60
Setting Response Headers
• Headers can be defined
– using setHeader(String header, String
value) of HTTPServletResponse
– using setIntHeader(String header, int
value)
– using setDateHeader(String header, long
milliseconds) (translate to GMT)
• The headers must be defined before the
first time the buffer content is sent
61
More Headers Methods
• containsHeader to check existence of
an header in the response
• setContentType
• setContentLength
• addCookie
62