Servlet interface:
Public interface Servlet
Its lifecycle is defined by the javax. Servlet. Servlet interface. When writing a Servlet, you must directly or indirectly implement this interface. Generally, it tends to be indirect: Derived from javax. Servlet. GenericServlet or javax. Servlet. http. HttpServlet. When implementing the Servlet interface, you must implement the following five methods:
1. init ():
Public void init (ServletConfig config) throws ServletException
Once the Servlet is instantiated, the container calls this method. The container traditionally provides this method to a ServletConfig object, so that the Servlet instance can save the configuration data related to the container for future use. If this method does not end normally, a ServletException will be thrown. Once this exception is thrown, the Servlet will no longer execute it, and subsequent calls to it will cause the container to reload it and run this method again. The interface specifies that this method can be called only once for any Servlet instance. Before any request is sent to the Servlet, this method can be completed without throwing an exception.
2. service ():
Public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException
This method can be called to process user requests only after successful initialization. The previous parameter provides methods and fields for accessing the initial request data, and the other provides the Servlet method for constructing the response.
3. destroy ():
Public void destroy ()
The container can terminate the Servlet service at any time. The container must give the service () thread enough time to end the execution before calling this method. Therefore, the interface specifies that destroy () is not executed when the service () is being executed.
4. getServletConfig ():
Public ServletConfig getServletConfig ()
During Servlet initialization, the container passes in a ServletConfig object and stores it in the Servlet instance. This object allows access to two items: the initialization parameter and the ServletContext object, the former is usually specified by the container in the file, which allows you to transmit scheduling information to the sevrlet at runtime, and the latter provides container information for the Servlet. This method allows the Servlet to obtain the object and configuration information at any time.
5. getServletInfo ():
Public String getServletInfo ()
This method returns a String object containing Servlet Information, such as developer, creation date, and description information. This method can also be used for containers.
GenericServlet class
Public abstract class GenericServlet implants Servlet, ServletConfig, Serializable
This class provides the basic implementation part of the Servlet interface, and its service () method is declared as abstract, so it needs to be derived. The init (ServletConfig conf) method stores the ServletConfig object in a private transient private temporary) instance variable. The getServletConfig () method returns a pointer to this object. If you reload this method, you cannot use getServletConfig to obtain the ServletConfig object. If you want to reload the object, remember to include. config. API version 2.1 provides an overloaded init () method without parameters. Now there is a call to init () at the end of the init (ServletConfig) method, although it is empty at present. In API version 2.1, this class implements the ServletConfig interface, which allows developers to directly call the ServletConfig method without obtaining the ServletConfig object. These methods are getInitParameter (), getInitParameterNames (), and getServletContext. This class also contains two log writing methods, which actually call the corresponding methods on ServletContext. The log (String msg) method writes the Servlet name and msg parameter to the container log. log (String msg, Throwable cause) contains an exception besides Servlet.
HttpServlet class
This class extends the GenericServlet class and provides more HTTP-related implementations for Servlet interfaces.
Service ():
Protected void service (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
Public void service (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
This method is used as the HTTP request distributor and cannot be overloaded at any time. When a request arrives, the service () method determines the request type GET, POST, HEAD, OPTIONS, DELETE, PUT, TRACE ), and distribute the request to the corresponding processing methods doGet (), doPost (), doHead (), doOptions (), doDelete (), doPut (), doTrace ()) each do method has the same form as the first service. To respond to specific types of HTTP requests, we must reload the corresponding do method. If the Servlet receives an HTTP request without reloading the corresponding do method, it returns a standard HTTP Error indicating that this method is unavailable to this resource.
GetLatModified ():
Protected long getLastModified (HttpServletRequest req)
This method returns the latest Servlet modification time dependent on January 1, 1970 00:00:00 GMT time in milliseconds. By default, a negative number is returned, indicating that the time is unknown. When processing a GET request, you can call this method to know the latest modification time of the Servlet, and the server can decide whether to remove the result from the cache.
HttpServletRequest Interface
Public interface HttpServletRequest extends ServletRequest
All objects that implement this interface, such as HTTP request objects passed from the Servlet container, can allow the Servlet to access the data of all requests through its own methods. The following are some basic methods for getting form data.
GetParameter ()
Public String getParameter (String key)
This method tries to locate the corresponding parameter based on the keyword in the query string and return its value. If multiple values exist, the first value in the list is returned. If no parameter is specified in the request information, null is returned.
GetParametervalues ():
Public String [] getParametervalues (String key)
If a parameter can return multiple values, such as a set of check boxes, you can use this method to obtain all values of the corresponding parameter. If no parameter is specified in the request information, null is returned.
GetParameterNames ():
Public Enumeration getParameterNames ()
This method returns an Enumeration object that contains a list of all parameter names of the corresponding request.
HttpServletResponse Interface
Public interface HttpServletResponse extends ServletResponse
The Servlet container provides an object that implements this interface and passes it to the Servlet through the service () method. Through this object and its method, Servlet can modify the response header and return results.
SetContentType ():
Public void setContentType (String type)
Before sending a response to the caller, you must use this method to set the MIME type of the HTTP response. It can be any valid MIME type. When HTML is returned to the browser, it is of the "text/html" type.
GetWriter ():
Public PrintWriter getWriter () throws IOException
This method returns the PrintWriter object and the Servlet result as text to the caller. The PrintWriter object automatically converts the UniCode characters in Java to the correct encoding for the client to read.
GetOutputStream ():
Public ServletOutputStream getOutputStream () throws IOException
This method returns the ServletOutputStream object, which is a subclass of java. io. OutputStream. This object sends binary data to the customer.
SetHeader ():
Public void setHeader (String name, String value)
This method is used to set the HTTP Response Header sent back to the customer. There are some quick methods to change some common response headers, but sometimes you need to call this method directly.
Compilation Conditions
You need to get a copy of JSDK from the http://java.sun.com/products/Servlet/ and move Servlet. jar to the \ jre \ lib \ ext directory under the JDK installation directory. If JDK1.1 is used, move to \ lib and add the absolute path of Servlet. jar to CLASSPATH.
Running Conditions
Apache Jserv, Jrun Servlet Exec, Java Web Server, Weblogic, WebSphere, Tomcat, Resin, and other Servlet Server programs are required.
Simple Example
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class HelloWorld extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
- {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("<html>");
- out.println("<body>");
- out.println("<head>");
- out.println("<title>Hello World!</title>");
- out.println("</head>");
- New Servlet and JSP features
- Talking about Servlet Web Server
- Implement Servlet applications in Weblogic
- Communication between Servlet and CGI
- Use MIDlet to activate Servlet