Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Servlet interfaces?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly talks about "what are the Servlet interfaces". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the Servlet interfaces.

Servlet interface:

Public interface Servlet

Its life cycle is defined by the javax.Servlet.Servlet interface. You must implement this interface directly or indirectly when you are writing Servlet. It generally tends to be implemented indirectly: by deriving from javax.Servlet.GenericServlet or javax.Servlet.http.HttpServlet. When implementing the Servlet interface, you must implement its five methods:

1.init ():

Public void init (ServletConfig config) throws ServletException

Once the Servlet is instantiated, the container calls this method. The container traditionally gives a ServletConfig object to this method so that an instance of Servlet can save the configuration data related to the container for later use. If this method does not end properly, a ServletException will be thrown. Once the exception is thrown, the Servlet no longer executes, and subsequent calls to it cause the container to reload it and run the method again. The interface specifies that this method can only be called once for any Servlet instance, and can be run without throwing an exception before any request is passed to Servlet.

2.service ():

Public void service (ServletRequest req,ServletResponse res) throws ServletException,IOException

Only after successful initialization can this method be called to process the user request. The former parameter provides the methods and fields to access the initial request data, and the latter provides the Servlet construction 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 finish execution before calling this method, so the interface specifies that destroy () is not executed while service () is executing.

4.getServletConfig ():

Public ServletConfig getServletConfig ()

During Servlet initialization, the container passes in a ServletConfig object and saves it in the Servlet instance, which allows access to two items: the initialization parameter, which is usually specified by the container in the file, and the ServletContext object, which allows scheduling information to be passed to sevrlet at run time, and the ServletContext object that provides Servlet with information about the container. This method allows Servlet to get the object and configuration information at any time.

5.getServletInfo ():

Public String getServletInfo ()

This method returns a String object that contains information about the Servlet, such as developer, creation date, description information, and so on. This method can also be used for containers.

GenericServlet class

Public abstract class GenericServlet implants Servlet,ServletConfig,Serializable

This class provides the basic implementation of the Servlet interface, whose service () method is declared abstract and therefore needs to be derived. The init (ServletConfig conf) method stores the ServletConfig object in a private transient (private temporary) instance variable, and the getServletConfig () method returns a pointer to this object. If you overload this method, you will not be able to use getServletConfig to get the ServletConfig object. If you do want to overload, remember to include a call to super.config. API version 2. 1 provides an overloaded init () method with no parameters. Now there is a call to init () at the end of the init (ServletConfig) method, although it is currently empty. In version 2.1 of API, this class implements the ServletConfig interface, which allows developers to directly call ServletConfig methods without getting ServletConfig objects: getInitParameter (), getInitParameterNames (), and getServletContext. This class also contains two methods for logging, which actually call the corresponding methods on the ServletContext. The log (String msg) method writes the name of the Servlet and the msg parameter to the container log, and log (String msg,Throwable cause) contains an exception in addition to Servlet.

HttpServlet class

This class extends the GenericServlet class and provides a more HTTP-related implementation of the Servlet interface.

Service ():

Protected void service (HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException

Public void service (HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException

This method acts as a dispatcher for HTTP requests, and it cannot be overloaded at any time. When the request arrives, the service () method determines the type of request (GET,POST,HEAD,OPTIONS,DELETE,PUT,TRACE) and distributes the request to the corresponding processing methods (doGet (), doPost (), doHead (), doOptions (), doDelete (), doPut (), doTrace ()) each do method has the same form as * service (). In order to respond to a specific type of HTTP request, we must overload the corresponding do method. If Servlet receives a HTTP request and you do not overload the corresponding do method, it returns a standard HTTP error indicating that the method is not available for this resource.

GetLatModified ():

Protected long getLastModified (HttpServletRequest req)

This method returns the time in milliseconds when the GMT was last modified since 00:00:00 on January 1, 1970. By default, it returns a negative number indicating that the time is unknown. When processing a GET request, call this method to know when the Servlet was last modified, 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 the HTTP request object passed from the Servlet container, allow Servlet to access all requested data through its own methods. Here are some basic methods for getting form data.

GetParameter ()

Public String getParameter (String key)

This method attempts to locate the corresponding parameter and return its value based on the keywords in the query string. If there is more than one value, the * * values in the list are returned. Returns null if no parameter is specified in the request information.

GetParametervalues ():

Public String [] getParametervalues (String key)

If a parameter can return multiple values, such as a collection of check boxes, you can use this method to get all the values of the corresponding parameter. Returns null if no parameter is specified in the request information.

GetParameterNames ():

Public Enumeration getParameterNames ()

This method returns an Enumeration object containing a list of all parameter names for the corresponding request.

HttpServletResponse interface

Public interface HttpServletResponse extends ServletResponse

The Servlet container provides an object that implements the interface and passes it to Servlet through the service () method. With this object and its method, Servlet can modify the response header and return the result.

SetContentType ():

Public void setContentType (String type)

You must use this method to set the MIME type of the HTTP response before sending the response back to the caller. It can be any valid MIME type, which is the "text/html" type when HTML is returned to the browser.

GetWriter ():

Public PrintWriter getWriter () throws IOException

This method returns the PrintWriter object and returns the result of the Servlet to the caller as text. The PrintWriter object automatically converts the UniCode-encoded characters inside the Java into the correct encoding so that the client can read it.

GetOutputStream ():

Public ServletOutputStream getOutputStream () throws IOException

This method returns a 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 that is sent back to the customer. There are some quick ways to change some commonly used response headers, but sometimes you need to call this method directly.

Compilation condition

You need to get a copy of JSDK from http://java.sun.com/products/Servlet/ and move Servlet.jar to the\ jre\ lib\ ext directory under the JDK installation directory. If it is JDK1.1, move to\ lib and add the absolute path of Servlet.jar in CLASSPATH.

Operation condition

Servlet server programs such as Apache Jserv,Jrun Servlet Exec,Java Web Server,Weblogic,WebSphere,Tomcat,Resin 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 ("")

Out.println ("")

Out.println ("")

Out.println ("Hello World!")

Out.println ("")

At this point, I believe you have a deeper understanding of "what are the Servlet interfaces?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report