Thursday, May 3, 2012
Tuesday, May 1, 2012
Introduction to Servlets
Introduction to Servlets
Servlets are modules that run inside request/response-oriented servers, such as Java-enabled web servers. Functionally they operate in a very similar way to CGI scripts, however, being Java based they are more platform independent.
Some Example Applications
A few of the many applications for servlets include,
- Processing data POSTed over HTTPS using an HTML form, including purchase order or credit card data. A servlet like this could be part of an order-entry and processing system, working with product and inventory databases, and perhaps an on-line payment system.
- Allowing collaboration between people. A servlet can handle multiple requests concurrently; they can synchronize requests to support systems such as on-line conferencing.
- Forwarding requests. Servlets can forward requests to other servers and servlets. This allows them to be used to balance load among several servers that mirror the same content. It also allows them to be used to partition a single logical service over several servers, according to task type or organizational boundaries.
Servlet Architecture Overview
The central abstraction in the Servlet API is the
Servlet
interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet
. The inheritance hierarchy looks as follows.
The
Servlet
interface provides the following methods that manage the servlet and its communications with clients.- destroy()
Cleans up whatever resources are being held and makes sure that any persistent state is synchronized with the servlet's current in-memory state. - getServletConfig()
Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet. - getServletInfo()
Returns a string containing information about the servlet, such as its author, version, and copyright. - init(ServletConfig)
Initializes the servlet. Run once before any requests can be serviced. - service(ServletRequest, ServletResponse)
Carries out a single request from the client.
When a servlet accepts a
service
call from a client, it receives two objects, ServletRequest
and ServletResponse
. The ServletRequest
class encapsulates the communication from the client to the server, while the ServletResponse
class encapsulates the communication from the servlet back to the client.
The
ServletRequest
interface allows the servlet access to information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. It also provides the servlet with access to the input stream,ServletInputStream
, through which the servlet gets data from clients that are using application protocols such as the HTTP POST and PUT methods. Subclasses of ServletRequest
allow the servlet to retrieve more protocol-specific data. For example, HttpServletRequest
contains methods for accessing HTTP-specific header information.
The
ServletResponse
interface gives the servlet methods for replying to the client. It allows the servlet to set the content length and mime type of the reply, and provides an output stream, ServletOutputStream
, and a Writer through which the servlet can send the reply data. Subclasses of ServletResponse
give the servlet more protocol-specific capabilities. For example, HttpServletResponse
contains methods that allow the servlet to manipulate HTTP-specific header information.
The classes and interfaces described above make up a basic Servlet. HTTP servlets have some additional objects that provide session-tracking capabilities. The servlet writer can use these APIs to maintain state between the servlet and the client that persists across multiple connections during some time period.
Servlet Lifecycle
Servers load and run servlets, which then accept zero or more requests from clients and return data to them. They can also remove servlets. These are the steps of a servlets lifecycle. The next paragraphs describe each step in more detail, concentrating on concurrency issues.
When a server loads a servlet, it runs the servlet's
init
method. Even though most servlets are run in multi-threaded servers, there are no concurrency issues during servlet initialization. This is because the server calls the init
method once, when it loads the servlet, and will not call it again unless it is reloading the servlet. The server can not reload a servlet until after it has removed the servlet by calling the destroy
method. Initialization is allowed to complete before client requests are handled (that is, before the service
method is called) or the servlet is destroyed.
After the server loads and initializes the servlet, the servlet is able to handle client requests. It processes them in its
service
method. Each client's request has its call to the service
method run in its own servlet thread: the method receives the client's request, and sends the client its response.
Servlets can run multiple
service
methods at a time. It is important, therefore, that service
methods be written in a thread-safe manner. If, for some reason, a server should not run multiple service
methods concurrently, the servlet should implement the SingleThreadModel
interface. This interface guarantees that no two threads will execute the servlet's service
methods concurrently.
Servlets run until they are removed from the service. When a server removes a servlet, it runs the servlet's
destroy
method. The method is run once; the server will not run it again until after it reloads and reinitializes the servlet.
The diagram below shows the basic interactions between clients, a web server, and a servlet registered with the web server.
When the
destroy
method runs, however, other threads might be running service requests. If, in cleaning up, it is necessary to access shared resources, that access should be synchronized. During a servlet's lifecycle, it is important to write thread-safe code for destroying the servlet and, unless the servlet implements theSingleThreadModel
interface, servicing client requests.Writing the Servlet
Servlets implement the
javax.servlet.Servlet
interface. While servlet writers can develop servlets by implementing the interface directly, this is usually not required. Because most servlets extend web servers that use the HTTP protocol to interact with clients, the most common way to develop servlets is by specializing the javax.servlet.http.HttpServlet
class. This tutorial concentrates on describing this method of writing servlets.
The
HttpServlet
class implements the Servlet interface by extending the GenericServlet base class, and provides a framework for handling the HTTP protocol. Itsservice
method supports standard HTTP/1.1 requests by dispatching each request to a method designed to handle it.
By default, servlets written by specializing the
HttpServlet
class can have multiple threads concurrently running its service
method. If, you would like to have only a single thread running a single service
method at a time, then in addition to extending the HttpServlet
, your servlet should also implement theSingleThreadModel
interface. This does not involve writing any extra methods, merely declaring that the servlet implements the interface.
For example,
public class SurveyServlet extends HttpServlet implements SingleThreadModel { /* typical servlet code, with no threading concerns * in the service method. No extra code for the * SingleThreadModel interface. */ ... }
Interacting with Clients
Servlet writers who are developing HTTP servlets that specialize the
HttpServlet
class should override the method or methods designed to handle the HTTP interactions that their servlet will handle. The candidate methods include,doGet
, for handling GET, conditional GET and HEAD requestsdoPost
, for handling POST requestsdoPut
, for handling PUT requestsdoDelete
, for handling DELETE requests
By default, these methods return a
BAD_REQUEST (400)
error. An example HTTP servlet that handles GET and HEAD requests follows; it specializes the doGet
method. The second example is also provided. It handles POST requests from a form by specializing the doPost
method.
The
HttpServlet
's service
method, by default, also calls the doOptions
method when it receives an OPTIONS request, and doTrace
when it receives a TRACE request. The default implementation of doOptions
automatically determines what HTTP options are supported and returns that information. The default implementation of doTrace
causes a response with a message containing all of the headers sent in the trace request. These methods are not typically overridden.
Whatever method you override, it will take two arguments. The first encapsulates the data from the client, and is an
HttpServletRequest
. The second encapsulates the response to the client, and is an HttpServletResponse
. The following paragraphs discuss their use.
An
HttpServletRequest
object provides access to HTTP header data, such as any cookies found in the request and the HTTP method with which the request was made. It, of course, allows the you to obtain the arguments that the client sent as part of the request. How you access the client data might depend on the HTTP method of the request.- For any HTTP method, you can use the
getParameterValues
method, which will return the value of a named parameter. (The methodgetParameterNames
provides the names of the parameters.) You can also manually parse the request. - For requests using the HTTP GET method, the
getQueryString
method will return aString
to be parsed. - For HTTP methods POST, PUT, and DELETE, you have the choice between two methods. If you expect text data, then it can be read using the
BufferedReader
returned by thegetReader
method, if you expect binary data, then it should be read with theServletInputStream
returned by thegetInputStream
method.
Note that you should use either the
getParameterValues
method or one of the methods that allow you to parse the data yourself. They can not be used together in a single request.
For responding to the client, an
HttpServletResponse
object provides two ways of returning the response data to the user. You can use the writer returned by thegetWriter
method or the output stream returned by the getOutputStream
method. You should use getWriter
to return text data to the user, andgetOutputStream
for binary data.
Before accessing the Writer or OutputStream, HTTP header data should be set. The
HttpServletResponse
class provides methods to access the header data, such as the content type and encoding, and content length of the response. After you set the headers, you may obtain the writer or output stream and send the body of the response to the user. Closing the writer or output stream after sending the response to the client allows the server to know when the response is complete.Example of an HTTP Servlet that handles the GET and HEAD methods
/** * This is a simple example of an HTTP Servlet. It responds to the GET * and HEAD methods of the HTTP protocol. */ public class SimpleServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // set header field first res.setContentType("text/html"); // then get the writer and write the response data PrintWriter out = res.getWriter(); out.println("<HEAD><TITLE> SimpleServlet Output</TITLE></HEAD><BODY>"); out.println("<h1> SimpleServlet Output </h1>"); out.println("<P>This is output is from SimpleServlet."); out.println("</BODY>"); out.close(); } public String getServletInfo() { return "A simple servlet"; } }
The example above shows the code for the entire servlet. The
doGet
method, because it is returning text to the client, uses the HttpServletResponse
'sgetWriter
method. It sets the response header field, content type, before writing the body of the response, and closes the writer after writing the response.
In addition to
doGet
, there is a second method, getServletInfo
. More information on the getServletInfo
method appears in later section. Because this servlet is an example shipped with the release, it is already compiled. To try the servlet, run it in the servletrunner section.Example of an HTTP Servlet that handles the POST method
The following example processes data POSTed by a form. The form looks like this:
<html> <head><title>JdcSurvey</title></head> <body> <form action=http://demo:8080/servlet/survey method=POST> <input type=hidden name=survey value=Survey01Results> <BR><BR>How Many Employees in your Company?<BR> <BR>1-100<input type=radio name=employee value=1-100> <BR>100-200<input type=radio name=employee value=100-200> <BR>200-300<input type=radio name=employee value=200-300> <BR>300-400<input type=radio name=employee value=300-400> <BR>500-more<input type=radio name=employee value=500-more> <BR><BR>General Comments?<BR> <BR><input type=text name=comment> <BR><BR>What IDEs do you use?<BR> <BR>JavaWorkShop<input type=checkbox name=ide value=JavaWorkShop> <BR>J++<input type=checkbox name=ide value=J++> <BR>Cafe'<input type=checkbox name=ide value=Cafe'> <BR><BR><input type=submit><input type=reset> </form> </body> </html>
The servlet writes the form data to a file, and responds to the user with a thank you message. The
doPost
method of the servlet looks like this:/** * Write survey results to output file in response to the POSTed * form. Write a "thank you" to the client. */ public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // first, set the "content type" header of the response res.setContentType("text/html"); //Get the response's PrintWriter to return text to the client. PrintWriter toClient = res.getWriter(); try { //Open the file for writing the survey results. String surveyName = req.getParameterValues("survey")[0]; FileWriter resultsFile = new FileWriter(resultsDir + System.getProperty("file.separator") + surveyName + ".txt", true); PrintWriter toFile = new PrintWriter(resultsFile); // Get client's form data & store it in the file toFile.println("<BEGIN>"); Enumeration values = req.getParameterNames(); while(values.hasMoreElements()) { String name = (String)values.nextElement(); String value = req.getParameterValues(name)[0]; if(name.compareTo("submit") != 0) { toFile.println(name + ": " + value); } } toFile.println("<END>"); //Close the file. resultsFile.close(); // Respond to client with a thank you toClient.println("<html>"); toClient.println("<title>Thank you!</title>"); toClient.println("Thank you for participating"); toClient.println("</html>"); } catch(IOException e) { e.printStackTrace(); toClient.println( "A problem occured while recording your answers. " + "Please try again."); } // Close the writer; the response is done. toClient.close(); }
The
doPost
method uses the getParameterNames
and getParameterValues
methods to get the form data. Because it returns text to the client, doPost
calls thegetWriter
method. It sets the sets the response header field, content type, before writing the body of the response, and closes the writer when the response is complete.Lifecycle Methods
Servlets that manage resources do so by overriding the lifecycle methods
init
and destroy
. These servlets might need to be given arguments at startup, in order to initialize correctly.
Overriding the Init
Method
During initialization, the servlet should prepare the resources it manages, to ready the servlet for accepting service requests. It can do this without regard for multi-threading concerns, since there is only a single thread running on behalf of the servlet during initialization. As soon as the
init
method returns, the servlet can receive client requests. If, for some reason, the servlet's required resources can not be made available (for example, a required network connection can not be established), or some other initialization error occurs that would make it impossible for the servlet to handle requests, the init
method should throw an UnavailableException
exception.
The
init
method takes a ServletConfig
object as a parameter. The method should save this object, so that it can be returned by the getServletConfig
method. The simplest way to do this is to have the new init
method call super.init
. If you do not do this, you should store the ServletConfig object yourself, and override the getServletConfig
method so that it can obtain the object from its new location.
An example
init
method follows. It is the init
method from the Survey Servlet, which accepts input from a form and stores it in a file. In order store the survey information, it needs a directory. It receives the directory as an initialization parameter; initialization parameters are discussed in the next section.public void init(ServletConfig config) throws ServletException { super.init(config); //Store the directory that will hold the survey-results files resultsDir = getInitParameter("resultsDir"); //If no directory was provided, can't handle clients if (resultsDir == null) { throw new UnavailableException (this, "Not given a directory to write survey results!"); }
As you can see, this
init
method calls the super.init
method to manage the ServletConfig object. The init
method also sets a field, resultsDir
, with the directory name that is provided as an initialization parameter. If no directory name was provided, the servlet throws an unavailable exception. If the init
method completes successfully, the servlet can then handle client requests.Initialization Parameters
The specification of initialization parameters is server-specific. For example, they are specified with a property when a servlet is run with the servlet runner. This tutorial contains a general explanation of properties, and how to create them.
However the initialization parameters are specified, they are always obtained the same way: with the
getInitParameter
method. This method takes the parameter name as an argument. The example init
method calls getInitParameter
. If, for some reason, you need to get the parameter names, you can get them with thegetParameterNames
method.
Overriding the Destroy
Method
When a server unloads a servlet, it calls the servlet's
destroy
method. The destroy
method should undo any initialization work and synchronize persistent state with the current in-memory state. This section begins with a description of how to write a simple destroy
method, then describes how to structure a servlet if threads running its service
method might still be running when the destroy
method is called.
Though it is often the case that a servlet that overrides the
init
method must also override the destroy
method to undo that initialization, this is not required. Because initialization involves reading a file and using its contents to initialize a shared data structure, there is no work to undo when the server is finished with the servlet.
For many servlets, however, there is initialization work that must be undone. For example, assume there is a servlet that opens a database connection during initialization. Its
destroy
method, shown as an example below, would close that connection./** * Cleans up database connection */ public void destroy() { try { con.close(); } catch (SQLException e) { while(e != null) { log("SQLException: " + e.getSQLState() + '\t' + e.getMessage() + '\t' + e.getErrorCode() + '\t'); e = e.getNextException(); } } catch (Exception e) { e.printStackTrace(); } }Coping with Service Threads at Servlet Termination
When a server removes a servlet, it typically calls destroy after all service calls have been completed, or a server-specific number of seconds have passed, whichever comes first. If your servlet has operations that take a long time to run (that is, they may run longer than the server's grace period), then threads could still be running when destroy is called. The servlet writer is responsible for making sure that any threads still handling client requests complete. The remainder of this section describes a technique for doing this.
A servlet with potentially long-running service requests should keep track of how many
service
methods are currently running. Its long-running methods should periodically poll to make sure that they should continue to run. If the servlet is being destroyed, then the long-running method should stop working, clean up if necessary, and return.
For example, the instance variable that counts the number of
service
methods running could be called serviceCounter
, and the indicator of whether the servlet is being destroyed could be an instance variable called shuttingDown
. Each variable should have its own set of access methods:public ShutdownExample extends HttpServlet {private int serviceCounter = 0; private Boolean shuttingDown; ... //Access methods for serviceCounter protected synchronized void enteringServiceMethod() { serviceCounter++; } protected synchronized void leavingServiceMethod() { serviceCounter--; } protected synchronized int numServices() { return serviceCounter; } //Access methods for shuttingDown protected setShuttingDown(Boolean flag) { shuttingDown = flag; } protected Boolean isShuttingDown() { return shuttingDown; } }
The
service
method should increment the service counter each time it is entered and decrement it each time it returns:protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { enteringServiceMethod(); try { super.service(req, resp); } finally { leavingServiceMethod(); } }
The
destroy
method should check the serviceCounter
, and if there are any long-running methods, set the shuttingDown
variable. This variable will let the threads still handling client requests know that it is time to shut down. The destroy
method should then wait for the service
methods to complete, in order to provide a clean shutdown.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(numService() > 0) { try { thisThread.sleep(interval); } catch (InterruptedException e) { } } }
Providing Information about the Servlet
Some applets and applications, for example, the Java Web Server Administration Tool, display information about a servlet. This information can include a short description of the purpose of the servlet, its author, and perhaps its version number. The Servlet API provides a method to return this information,
getServletInfo
. By default, this method returns null. While servlet writers are not required to override this method, it is strongly recommended. The simple servlet, shown as an example earlier, overrides this method:/** * This is a simple example of an HTTP Servlet. It responds to the GET * and HEAD methods of the HTTP protocol. */ public class SimpleServlet extends HttpServlet { ... public String getServletInfo() { return "A simple servlet"; } }
How to use servletrunner
to Run a Servlet
Once you have written your servlet, you can run it in many web servers, or in the
servletrunner
. Where ever you decide to run your servlet, there are certain pieces of data that you might want or need to specify. When you are using servletrunner
you do this with properties. The next section describes a servlet's properties, and how to store them. Following that, there is a section on how to run servlets in servletrunner
.Properties
Properties are key-value pairs, used for the configuration, creation, and initialization of a servlet. For example,
servlet.Assignment.code=AssignmentServlet
is a property whose key is servlet.Assignment.code
and whose value is AssignmentServlet
.
There are two properties for servlets. One is
servlet.name.code
, whose value is the servlet's class name. The other property is servlet.name.initargs
, whose value holds the initialization parameters for the servlet. Both properties, servlet.name.code
and servlet.name.initargs
, are discussed in more detail below.
The
servlet.name.code
property names your servlet by associating its name with its class. If your servlet uses initialization parameters, this property is required. It allows the server to associate the servlet object with its initialization arguments: they both have the same name. Even if your servlet does not use initialization parameters, it is recommended that it have this property, so that clients can access the servlet using its name.
The value of the servlet.name.initArgs property holds the servlet's initialization parameters. The syntax of a single parameter is parameterName=parameterValue. The entire property (the entire key-value pair) must be a single logical line. For readability, you can use the backquote syntax to allow the property to span multiple lines in the file. For example, the argument to the Assignment servlet looks like this:
servlet.Assignment.initArgs=\ AssignmentParameter=ParameterValueThe Property File
Properties are stored in a file that is, by default, called "servlet.properties", though you can specify another name when
servletrunner
is started. The file should hold the properties for all the servlets that servletrunner
will run. It should be plain text. You can create it in an editor.Using Servlet Runner
If you would like to run your servlet in a web server, please see that server's documentation for instructions. This section explains how to run the servlet in the
servletrunner
utility that comes with this release.
The
servletrunner
is a small utility, intended for testing. It is multithreaded, so it can run more than one servlet. It can be used, therefore, to run multiple servlets simultaneously, or to test one servlet that calls other servlets in order to satisfy client requests. Unlike some web servers, it does not automatically reload servlets when they are updated. Because it is small, however, there is very little overhead associated with stopping and restarting it in order to use a new version of a servlet.
The
servletrunner
is in the <JDK>/bin
directory. Invoking it with the -help flag shows a usage message without running it:% ./bin/servletrunner -helpUsage: servletrunner [options] Options: -p port the port number to listen on -b backlog the listen backlog -m max maximum number of connection handlers -t timeout connection timeout in milliseconds -d dir servlet directory -r root document root directory -s filename servlet property file name -v verbose output %
In order to see the default values of these options, you can call
servletrunner
with the -v switch. This will, however, start the servlet runner. Just stop it after you have obtained the information, if you are not ready to run it yet, or want it to run with something other than the default values.
Suppose we choose port 5050. Then we can start servletrunner as follows.
./bin/servletrunner -p 5050 -d . -r . &
Once the servletrunner is executing, you run servlets by calling them directly in your browser, or as the forms example shows, by using a form that calls a servlet to process its data. The URL for a servlet has the following general form:
http://machine-name:port/servlet/servlet-name
where servlet-name corresponds to the name you have given your servlet. For example, to run the Assignment Servlet, which has the property
servlet.Assignment.code=AssignmentServlet
, you would use the following URL. (It assumes that servletrunner is running on machine(info.cse.iitb.ac.in), at port 5050, and that the Assignment servlet is located in the servlet directory provided to servletrunner
at startup:http://info.cse.iitb.ac.in:5050/servlet/Assignment
http://www.istutorials.com/java.htm http://www.interview-questions-java.com/ http://www.javaprogrammingworld.com/ http://www.daniweb.com/ http://kickjava.com/ http://www.java-samples.com http://videoworld-rong.blogspot.com http://www.itjungle.com http://klavanya.wordpress.com http://www.sitepoint.com/forums http://articles.sitepoint.com http://www.johntopley.com/ http://bitmeta.org/ http://www.java201.com http://www.corej2eepatterns.com http://www.cs.wcupa.edu/rkline/Java/jstl-examples.html http://www.experts-exchange.com http://www.developer.com/ http://www.devsphere.com/ http://www.ngri.org.in/alkacon-documentation/examples_jstl/ http://www.kodejava.org http://technosatish.blogspot.com/ http://www.exampledepot.com http://www.datadirect.com http://jobz9.com/ http://www.mainframegurukul.com/ http://www.alexwestconsulting.com http://www.orafaq.com/ http://www.objecttraininggroup.com/ www.seekjava.info www.freewebschools.com http://javapapers.com http://java-j2ee-interview-questions.blogspot.com/ http://www.java-questions.com/ http://java-interview-questions-bank.blogspot.com http://www.codeproject.com/ http://www.codegravity.com/ http://www.javadb.com/ http://www.devdaily.com/java/ http://www.devdaily.com/java/java-developer-jobs-programming-jobs http://pronetbeans.com/ http://demo.raibledesigns.com/struts-menu/index.jsp http://www.devarticles.com http://passion4java.blogspot.com
Servlet Overview and Architecture
|
- A client sends an HTTP request to the server . The server receives the request and directs it to be processed by the appropriateservlet. The servlet does its processing, which may include interacting with a database or other server-side components such as other servlets, JSPs or Enterprise Java-Beans. The servlet returns its results to the client—normally in the form of an HTML, XHTML or XML document to display in a browser, but other data formats, such as images and binary data, can be returned.
Servlet Life Cycle
- All servlets must implement the Servlet interface. As with many key applet methods, the methods of interface Servlet are invokedautomatically by the server. This interface defines five methods :
void init( ServletConfig config )
|
This method is automatically called once during a servlet’s execution cycle to initialize the servlet. The ServletConfig argument is supplied by the servlet container that executes the servlet.
|
ServletConfig getServletConfig()
| This method returns a reference to an object that implements interface ServletConfig. This object provides access to the servlet’s configuration information such as servlet initialization parameters and the servlet’s ServletContext, which provides the servlet with access to its environment (i.e., the servlet container in which the servlet executes). |
String getServletInfo()
| This method is defined by a servlet programmer to return a String containing servlet information such as the servlet’s author and version. |
void service( ServletRequest request, ServletResponse response )
| The servlet container calls this method to respond to a client request to the servlet. |
void destroy()
|
This “cleanup” method is called when a servlet is terminated by its servlet container. Resources used by the servlet, such as an open file or an open database connection, should be deallocated here.
|
- A servlet’s life cycle begins when the servlet container loads the servlet into memory—normally, in response to the first request that the servlet receives.
- Before the servlet can handle that request, the servlet container invokes the servlet’s init method.
- After init completes execution, the servlet can respond to its first request.
- All requests are handled by a servlet’s service method, which receives the request, processes the request and sends a response to the client.
- During a servlet’s life cycle, method service is called once per request. Each new request typically results in a new thread of execution (created by the servlet container) in which method service executes.
- Starting a new thread for each request is more efficient than starting an entirely new process,
- When the servlet container terminates the servlet, the servlet’s destroy method is called to release servlet resources.
- The servlet packages define two abstract classes that implement the interface Servlet—class GenericServlet (from the packagejavax.servlet) and class HttpServlet (from the package javax.servlet.http). These classes provide default implementations of all theServlet methods. Most servlets extend either GenericServlet or HttpServlet and override some or all of their methods.
- The key method in every servlet is service, which receives both a ServletRequest object and a ServletResponse object. These objects provide access to input and output streams that allow the servlet to read data from the client and send data to the client. These streams can be either byte based or character based. If problems occur during the execution of a servlet, either ServletExceptions or IOExceptionsare thrown to indicate the problem.
- Servlets can implement tagging interface javax.servlet.SingleThreadModel to indicate that only one thread of execution may enter method service on a particular servlet
instance at a time. When a servlet implements SingleThreadModel, the servlet container can create multiple instances of the servlet to handle multiple requests to the servlet
in parallel. In this case, you may need to provide synchronized access to shared resources used by method service.
HttpServlet Class
|
- public abstract class HttpServlet extends GenericServlet
- GenericServlet implements interface javax.servlet.Servlet.
- Class HttpServlet overrides method service to distinguish between the typical requests received from a client Web browser.
HTTP request types
|
description
| Common uses |
get | A get request gets (or retrieves) information from a server. | retrieve content of a specified URL such as HTML , XHTML document (i.e., a Web page) or an image. |
post | A post request posts (or sends) data to a server. | send information, such as authentication information or data from a form that obtains user input, to a server. |
- Class HttpServlet defines methods doGet and doPost to respond to get and post requests from a client, respectively.
- These methods are called by the service method, which is called when a request arrives at the server.
- Method service first determines the request type, then calls the appropriate method for handling such a request.
public void service(ServletRequest req,ServletResponse res) |
Dispatches client requests to the protected service method. There's no need to override this method.
|
protected void service(HttpServletRequest req, HttpServletResponse resp) | Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class. This method is an HTTP-specific version of the Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse) method. There's no need to override this method. |
Do not override method service in an HttpServlet subclass. Doing so prevents the servlet from distinguishing between request types.
|
- Methods of class HttpServlet that respond to the other request types are shown in the next figure. They all receive parameters of typeHttpServletRequest and HttpServletResponse and return void.
- Methods doGet and doPost receive as arguments an HttpServletRequest object and an HttpServletResponse object that enable interaction between the client and the server.
- The methods of HttpServletRequest make it easy to access the data supplied as part of the request.
- The HttpServletResponse methods make it easy to return the servlet’s results to the Web client.
Method | Description |
doDelete | Called in response to an HTTP delete request. Such a request is normally used to delete a file from a server. This may not be available on some servers, because of its inherent security risks (i.e., the client could delete a file that is critical to the execution of the server or an application). |
doOptions | Called in response to an HTTP options request. This returns information to the client indicating the HTTP options supported by the server, such as the version of HTTP (1.0 or 1.1) and the request methods the server supports. |
doPut | Called in response to an HTTP put request. Such a request is normally used to store a file on the server. This may not be available on some servers, because of its inherent security risks (i.e., the client could place an executable application on the server, which, if executed, could damage the server— perhaps by deleting critical files or occupying resources). |
doTrace |
Called in response to an HTTP trace request. Such a request is normally used for debugging. The implementation of this method automatically
returns an HTML document to the client containing the request header information (data sent by the browser as part of the request). |
HttpServletRequest Interface
|
- Every call to doGet or doPost for an HttpServlet receives an object that implements interface HttpServletRequest.
- The Web server that executes the servlet creates an HttpServletRequest object and passes this to the servlet’s service method (which, in turn, passes it to doGet or doPost)
- This object contains the request from the client. A variety of methods are provided to enable the servlet to process the client’s request. Some of these methods are from interface ServletRequest—the interface that HttpServletRequest extends.
Method | Description |
String getParameter( String name ) | Obtains the value of a parameter sent to the servlet as part of a get or post request. The name argument represents the parameter name. |
Enumeration getParameterNames() | Returns the names of all the parameters sent to the servlet as part of a post request. |
String[] getParameterValues( String name ) | For a parameter with multiple values, this method returns an array of Strings containing the values for a specified servlet parameter. |
Cookie[] getCookies() | Returns an array of Cookie objects stored on the client by the server. Cookies can be used to uniquely identify clients to the servlet. |
HttpSession getSession( boolean create ) | Returns an HttpSession object associated with the client’s current browsing session. An HttpSession object can be created by this method (true argument) if an HttpSession object does not already exist for the client. HttpSession objects can be used in similar ways to Cookies for uniquely identifying clients. |
HttpServletResponse Interface
|
- This object contains the response from the server.
void addCookie( Cookie cookie ) | Used to add a Cookie to the header of the response to the client. The Cookie’s maximum age and whether Cookies are enabled on the client determine if Cookies are stored on the client. |
ServletOutputStream getOutputStream() | Obtains a byte-based output stream for sending binary data to the client. |
PrintWriter getWriter() | Obtains a character-based output stream for sending text data to the client. |
void setContentType( String type ) | Specifies the MIME type of the response to the browser. The MIME type helps the browser determine how to display the data (or possibly what other application to execute to process the data). For example, MIME type "text/html" indicates that the response is an HTML document, so the browser displays the HTML page. For more information on |
Thread Life Cycle
Thread is a lightweight process, the smallest unit of scheduled execution. Instance of the Thread class in Java 6 could be in one of the following states:
- new,
- runnable,
- timed waiting,
- waiting,
- blocked,
- terminated.
These states are Java Virtual Machine (JVM) states reported by JVM to Java programs. At any given point in time thread could be in only one state.
Protocol State Machine Example - Thread States and Life Cycle in Java 6
New is the thread state for a thread which was created but has not yet started.
At the lower operating system (OS) level, JVM’s runnable state could be considered as a composite state with two substates. When a thread transitions to the runnable JVM state, the thread first goes into the ready substate. Thread scheduling decides when the thread could actually start, proceed or be suspended. Thread.yield() is explicit recommendation to thread scheduler to pause the currently executing thread to allow some other thread to execute.
A thread in the runnable state is executing from the JVM point of view but in fact it may be waiting for some resources from the operating system.
Timed waiting is a thread state for a thread waiting with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
- Thread.sleep(sleeptime)
- Object.wait(timeout)
- Thread.join(timeout)
- LockSupport.parkNanos(timeout)
- LockSupport.parkUntil(timeout)
A thread is in the waiting state due to the calling one of the following methods without timeout:
- Object.wait()
- Thread.join()
- LockSupport.park()
Note, that thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate. It means that waiting state could be made a composite state with states corresponding to these specific conditions.
Thread is in the blocked state while waiting for the monitor lock to enter a synchronized block or method or to reenter a synchronized block or method after calling Object.wait().
A synchronized statement or method acquires a mutual-exclusion lock on behalf of the executing thread, executes a block or method, and then releases the lock. While the executing thread owns the lock, no other thread may acquire the lock and is blocked waiting for the lock.
After thread has completed execution of run() method, it is moved into terminated state.
Subscribe to:
Posts (Atom)