Two basic packages for all servlets are required when writing Servlet: javax.servlet and javax.servlet.http. The following mainly introduces the HTTP Servlet application programming interface provided by javax.servlet.http.
First introduce the basic method of Java servlet.
Init () method In the lifecycle of the servlet, only the init () method is executed, that is, executing when the server is loaded with servlet. By configuring the server, you can set the servlet when the startup server or client accesss servlet. No matter how many client access servlets, INIT () will not be repeated. The service () method service () method is the main part of the servlet. The customer's service () method of the object will call each request of an HTTPSERVLET object, and passed to this method a "request" object and a "response" as a parameter. The "Request" object provides information about the request, and the "response" is provided with a communication path that returns the response information to the browser. The related class in the Javax.Servlet package is servletRequest and ServletResponse, and the related class in the Javax.Servlet.http package is HttpServletRequest and HttpservletResponse. Servlet communicates with the server through these objects and eventually communicates with the client. The servlet can know the client environment, the information of the server environment and all the information provided by the client, and all the information provided by the client; the servlet can send a response to the client by calling the "Response" object. The service () method in the httpservlet is the default service function is to call the corresponding DO functionality corresponding to the HTTP request. For example, if the HTTP request method is GET, DOGET () is called by default. When a client issues an HTTP POST request via an HTML form, the dopost () method is called. Parameters associated with the POST request are sent from the browser to the server as a separate HTTP request. DOPOST () method should be used when you need to modify the data of the server. The response of the servlet can be the following types: · An output stream, the browser explains according to its content type (such as Text / HTML). · An HTTP error response, redirect to another URL, Servlet, and JSP. The destroy () method destroy () method is only executed, ie, execute the method when the server is stopped and unresses the servlet. When the server unresses the servlet, the destroy () method will be called after all service () method calls are completed, or the destroy () method is called after the specified time interval. A servlet may generate other threads when run a service () method, so when calling the destroy () method, you must confirm that these threads have terminated or complete. GetServletConfig () Method GetServletConfig () method returns a servletconfig object that is used to return to initialization parameters and servletContext. The servletContext interface provides environmental information about servlet. GetServletInfo () Method GetServletInfo () method is an optional method that provides information about servlet, such as author, version, copyright.
Here is the steps to prepare a basic HTTP Servlet.
(1) Introduce the corresponding packages and classes, including:
Import javax.servlet. *;
Import javax.servlet.http. *; import java.io. *;
Inherit javax.servlet.http.httpservlet
Public class myservlet extends httpservlet {
(2) Implement the service method.
The main function of the servlet is to accept the HTTP request sent from the browser and return to the HTTP response (Response). This work is done in the service method. The Service method includes obtaining client data from the Request object and creates an output to the Response object.
If a servlet is inherited from javax.servlet.http.httpservlet, the Dopost or Doget method is implemented, then this servlet can only respond to POST or GET. If the developer wants to handle all types of requests, as long as the service method is simply implemented (but if you choose to implement the service method, you don't have to implement the Dopost or Doget method, unless you call Super.Service ()) . The difference is shown in Figure 14-2.
Figure 14-2 Differences between doget, dopost, service
The HTTP Servlet specification describes methods for processing types of other requests, and all of these methods can belong to the service method. All service methods use the same parameters. HTTPSERVLETREQUEST provides information about requests, and servlets can respond to HTTP clients using HTTPSERVLETRESPONSE.
Public Void Service (httpservletRequest Req, httpservletResponse res)
THROWS IOEXCEPTION {
/ / Set the type of response content
Res.SetContentType ("text / html");
// Get a reference to the java.io.printwriter object, used to output
PrintWriter out = res. maxwriter ();
// Create some HTML code using the println () method of the PrintWriter object, for example
Out.println ("
Out.println ("
......
}
(3) Compiling the servlet.
Compiling this servlet to the web-inf / class directory containing this Servlet source code file. Such as:
Javac -d / Your_Application_Dir / web-inf / class you_servlet.java
(4) Deploy this servlet as part of the application.
Do not turn on the program after compiling the program to the specified directory, we must configure it, there is a web.xml file in c: /bea/wlserver6.1/config/mydomain/applications/defaultwebapp/web-inf, this It is the profile of the application. The general form of this file is as follows:
XML Version = "1.0"?>
Application 1.2 // en "" http://java.sun.com/j2ee/dtds/web-app_2_2.dtd ">
init-param>
init-param>
servlet>
servlet-maping>
welcome-file-list>
web-app>
Where servlet-name is the name of the servlet, servlet-class is a relative path to the Class file in / web-inf / classes. We can also initialize parameters in this file, as follows:
init-param>
Param-name is the name of the parameter, which is Greeting in this example; param-value is the value of the parameter, in this case is Welcome. We can initialize more parameters in this form.
servlet-maping>
This code is a mapping of the servlet name with the URL-Pattern path. According to this code, the URL path of the HelloWorld2 servlet program is http: // server_address:
/ Helloworld2.
(5) Access the servlet from the browser. Generally speaking, calling Servlet's URL depends on the name of the servlet mapping in the web application deployment description containing the web application of the servlet. Request parameters can also be part of the URL calling servlet, usually the URL of the servlet as the following mode: http: // server_address:
/ your_web_application_name / mapped_servlet_name? parameter