Introduction to Servlet
table of Contents:
About servlets
Example Structure Description Lifecycle how to write a servlet program
The client's interactive Lifecycle provides information about servletrunner to run a servlet program with servletrunner.
About servlets
Servlets is a new new feature in Java 2.0.
Java Servlets is a module running on the request /-oriented request server, such as a Java-EnableD web server, and a similar extension. For example, a servlet can get data from an HTML order table and then use some commercial algorithms. Update the company's appropriate order database.
That is to say: Servlet can expand the web server function like the CGI script, but servlet takes up few intensive resources, there are many sites prepared using CGI scripts, due to the increase in visits, performance is rapidly decline, this is a shortcoming of CGI scripts, related CGI script concept, please refer to our "CGI Getting Started". At the same time, because servlets are written in Java, it is a cross-platform. The actual servlet is the real start of e-commerce.
Servlet API, is used to write servlet, write servlets, such as if you have a servlet that is loaded, what is the server environment that is loaded, or the protocol used to transfer data, so servlets Can be fused in different web servers.
Servlet can be quite effectively replaced with CGI scripts: it can easily have easy to write and run fast text. It can be easily commissioned to find out programs. The servlet program is developed with Java Servlet API, A Standard Java Extension. But not Part of the Java core framework can be used as a common additional product package to be used by merchants.
Example
Here are some servlet applications:
Used to process the HTML form generates Posted data through HTTPS, including trading order or credit card data. Therefore, servlet can become part of the order processing system, and work with the product inventory database, may be available on the online payment system. Allow people to cooperate between people A servlet can process multiple requests; they can use synchronous request support systems such as online meetings. Transfer requests. Servlet can transfer requests to other servers and servlets. This allows several servers on the same content in mirror Between balanced load. According to the type or organization range, it can be allowed to be used to divide logically in several servers. Servlet writers can define the activation agents that work together with each other, each agent is a servlet And the agent can transfer data between them.
Servlet structure
Before you have the specific servlet, you must understand the Java language. The following is based on your understanding of Java, the most important thing in the Servlet API is the servlet interface. All servlets IMPLEMENT (execution) this interface, the way, or directly, or via the extending Class, such as httpservlet This servlet interface provides methods for arranging Servlet and client. Servlet writers can provide more or all of the following methods when they develop Servlet programs.
When a servlet receives a call request from the client, it receives two objects: one is servletRequest, and the other is servletResponse. This servleTRequest class is summarized from the client to the server, and the servletResponse Class is summarized from servlet to the client connection.
ServletRequest Interface can get such information, such as the elaboration name transmitted by the client, the client is using the protocol, generates a request and receives a remote host name of the requested server. It also provides servlet, servletinputstream, servletinputStream, these data is The HTTP POST and PUT methods are submitted in client references. A substext of a servletRequest allows the servlet to get more protocol characteristics. For example: HTTPSERVLETREQUEST contains methods for obtaining HTTP-SPECIFIC header information .ServletResponse interface gives the corresponding client Servlet method. It allows servlet to set content length and response MIME type, and provide output streams, servletOutputStream, can send back the corresponding data by writers. ServletResponse subclasses can give more Protocol-Specific capacity information. For example: httpservletResponse contains methods that allow servlets to operate HTTP-SPECIFIC header information.
About classes and interfaces describes a basic servlet framework. HTTP servlets have some additional methods that provide session-tracking capabilities. Servlet writers can use these APIs to maintain the status between servlets and clients when there are others .
Servlet Lifecycle
Server load run servlets: Receive multiple requests from the client and return data to the client. Then delete the servlets. This is the servlets lifecle process. Detailed description below:
When a server loads servlet, it runs a servlet's init method. This method cannot be called repeatedly. Once the call is loaded, the servlet is loaded until the server calls the Destroy method to call the servlet before call.
After server loading initialization, servlet, servlet can handle the client's request. Use the service method to do this. Each client request has its own service method: These methods receive the client request and send back the corresponding response.
Servlets can run multiple service at the same time. This is important, so that the service method can be written in a Thread-Safe style. For example, the service method updates a field field in the servlet object, this field can be accessed at the same time. If a certain A server cannot run a service method at the same time, or you can use SingLethreadModel Interface. This interface is guaranteed to have more than two threads Threads.
Servlets has been running until they are uninstalled by the server. In Servlet's Lifecycle, write a Thread-SAFE encoding to uninstall servlet is important.
Write servlet
Servlets executes javax.servlet.servlet interface. When servlet writers develop servlets by direct import interface, this is usually not necessary. Because most servlets are for web servers with HTTP protocols, the most universal development servlet method is to use Javax. Servlet.http.httpservlet.
The HTTPSERVLET class performs servlet interface through the Extend GenericServlet class, provides the ability to process the HTTP protocol. His service method supports standard HTTP / 1.1 requests.
Generally, servlets written in the class specified by httpservlets can run the service method in multiple threads.
Interactivity with the client
Servlet Writer Note that there are several ways to lack your httpservlet class, you can define the contents of the method, but you must use these method names to make the servlet know what you want,
DOGET, used to process GET, conditional GET and header HEAD request DOPOST, user handles POST request DOPUT, to process the PUT request Dodelete, to process the delete request Httpservlet's service method, generally, when it receives an Options When the request is called, the DOOPTIONS method is called when receiving a TRACE request is called dotrace. Dooptions default execution method is automatically determined what kind of HTTP is selected and is returned which information.
When you use these methods, you must take two elaboration. The first data containing data from the client HTTPSERVLETREQUEST. The second parameter contains the client's response httpservletResponse. In the following example, this is the case.
An HTTPSERVLETREQUEST object provides access to the HTTP header, also allows you to get the client's data. How to get these data depends on the HTTP side request method.
Regardless of any HTTP mode, you can use the getParameterValues method, this parameter value used to return a specific name. For the way with the HTTP GET request, this getQueryString method will return to anatomy. For anatomy, for anatomy, And the way you request it, you have two ways to choose. If you are text data, you can get BufferedReader through the getReader method; if you are binary data, you can get servletinputStream with the GetReader method.
In response to the client, an HttpservletResponse object provides two ways to return data to the user. You can return to the getWriter method, or the getOutputStream method returns to the output stream. You should use getWriter to return text data, and return binary data with GetOutputStream.
Before using Writer or OutputStream, the HTTP header should be set first. This method is provided in HTTPServletResponse, which can then use Writer or OutputStream to send a response body portion back to the user. After completion, turn off the Writer or Output Stream so that the server knows that the response has been complete.
An example of a HTTP Servlet handling GET and HEAD methods
Public class simpleServlet Extends httpservlet {
Public void doget (httpservletRequest Req, httpservletResponse res)
Throws ServleTexception, IOException
{
// First set the head
Res.SetContentType ("text / html");
// Return response data with Writer method
PrintWriter out = res. maxwriter ();
Out.println ("
Out.println ("
Out.println ("
this is output is from limited");
Out.println (" Body>");
Out.close ();
}
Public string getServletInfo () {
Return "a Simple servlet";
}
}
This example fully realized a servlet.
An example of an HTTP Servlet handling the POST method
Here is an example of using an HTML with a POST form: