Download presentation
Published byAsher Richard Modified over 9 years ago
1
Representation and Management of Data on the Web
Servlets Representation and Management of Data on the Web
2
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
3
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
4
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:
5
A Java Servlet Servlet Web browser Web server request request response
6
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!
7
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
8
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
9
Object Hierarchy Servlet Generic Servlet HttpServlet YourOwnServlet
10
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
11
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, doGet(req, res);
12
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?
13
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(); }
15
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)
16
Servlet Life Cycle Deal with requests: call the service method
Load into Web server Destroy the servlet: call the destroy method Calling the init method ServletConfig Garbage Collection Servlet Class
17
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)
18
A Glance at Servlet Configuration Parameters
<?xml version="1.0" encoding="ISO "?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.2//EN" " <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>
19
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import 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 + }
20
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>"); }
21
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?
22
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
23
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
24
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?
25
Instance of your Servlet
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 thread 3 Instance of your Servlet thread 2
26
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 { … }
27
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
28
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.
29
Instance of your Servlet Instance of your Servlet
thread 4 The Two Options thread 1 Pool of Instances Instance of your Servlet thread 3 Instance of your Servlet Queue thread 3 thread 2 Instance of your Servlet thread 1 Instance of your Servlet thread 2 Instance of your Servlet thread 3 Instance of your Servlet thread 4 Instance Per Thread
30
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
31
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?
32
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;
33
protected void service(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException { enteringServiceMethod(); try { super.service(req, resp); } finally { leavingServiceMethod(); }
34
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;
35
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) {}
36
public void doPost(...) { ... for(i = 0; ((i < numberOfThingsToDo) && !isShuttingDown()); i++) { try { partOfLongRunningOperation(i); } catch (InterruptedException e) {} }
37
Getting Information From the User
38
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
39
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
40
Example http://pita.cs.huji.ac.il:8090/servlet/update?x=19&y=104
<HTML> … <form method=“GET” action=“ <INPUT name=“x” type=“text”> <INPUT name=“y” type=“text”> <INPUT type=“submit”> <INPUT type=“reset”> </form> </HTML>
41
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()
42
<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>
44
SetColors?bgcolor=wheat&fgcolor=blue&size=5
45
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");
46
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>"); }
47
Think About it Could you have implemented this example (or something similar) using Javascript? Should you use Javascript for something like this? When?
48
<FORM ACTION=“dbi/servlet/SetColors” METHOD=“POST”>
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); }
49
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.
50
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import 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";
51
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");
52
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);
55
Creating the Response of the Servlet
56
HTTP Response The response includes:
Status line: version, status code, status message Response headers Empty line Document HTTP/ OK Content-Type: text/plain Bla Bla Bla
57
Response is Buffered Servlet server Buffer response client request
setBufferSize getBufferSize flushBuffer etc. client request
58
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
59
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
60
Example of Using Redirect
Any ideas? (Remember ex2...)
61
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
62
More Headers Methods containsHeader to check existence of an header in the response setContentType setContentLength addCookie
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.