Servlet & Jsp-servlet API Overview

Source: Internet
Author: User

Servlet & Generic & HttpServlet class diagram

The life cycle of the Servlet

Init, service, and destroy are the life cycle methods of the servlet, and their invocation rules are as follows:

Init: The servlet container calls this method when the servlet is requested for the first time, and the container will no longer call this method even if the request is received later. Therefore, you can do some initialization operations within the Init method. When this method is called, the container passes a servletconfig parameter, which can be reserved using a class-level variable. Servlets are not singleton, but the servlet container constructs only one instance for each servlet class.

Service: This method is called by the servlet container when a request is received.

Destory: This method is called by the container when the servlet is destroyed. This is usually when the Web app is unloaded from the servlet container or the servlet container is closed. Some cleanup operations can be done in the Destroy method.

Load-on-startup parameters

By default, the servlet container will not construct and initialize the servlet instance until the servlet receives the request for the first time. Using the Load-on-startup parameter, you can specify that the servlet is loaded when the container starts. The Load-on-startup parameter is especially useful when the initialization operation is complex and takes a long time. Load-on-startup is an integer parameter that specifies the order in which the container loads the servlet. When the value is less than 0 or unspecified, the servlet container can choose to instantiate the servlet at any time. When a value is greater than or equal to 0 o'clock, the smaller the value, the more precedence the servlet container instantiates the servlet. As Load-on-startup, the servlet container chooses the order in which the servlet is loaded. The Load-on-startup parameter is specified by @WebServlet annotations, but only

<servlet>    <Servlet-name>Myservlet</Servlet-name>    <Servlet-class>Com.huey.hello.servlets.MyServlet</Servlet-class>    <Load-on-startup>1</Load-on-startup></servlet>

ServletRequest & HttpServletRequest

ServletRequest (HttpServletRequest) encapsulates an (HTTP) request. Here are the methods commonly used by ServletRequest (HttpServletRequest):

Getservletcontext: Gets the servlet context instance.

GetSession: Gets the session instance.

GetCookies: Get cookies.

GetHeader: Gets the header of the HTTP request. A similar approach is getintheader. There are some ways to get specific headers: Getcontentlength, getContentType, and so on.

GetParameter: Gets the request parameters for the form and URL. Similar methods include Getparametervalues, Getparameternames, and Getparametermap.

GetAttribute: Gets the requested property.

GETREMOTEADDR: Returns the IP address of the client or the last agent that sent the request.

Getremotehost: Returns the fully qualified name of the client sending the request or the last agent.

Servletresponse & HttpServletResponse

Servletresponse (HttpServletResponse) encapsulates an (HTTP) response. Here are the methods commonly used by Servletresponse (HttpServletResponse):

setContentType: Sets the content of the response.

Addcookie: Adds the specified cookie to the response.

AddHeader: Adds the specified header to the response.

ServletConfig

When invoking the servlet's Init method, the Servlet container passes a ServletConfig object, ServletConfig encapsulates the configuration information for the servlet. There are two ways to configure the initial parameters of a servlet.

Configure the servlet's initial parameters in Web. xml:

<servlet>    <Servlet-name>Myservlet</Servlet-name>    <Servlet-class>Com.huey.hello.servlets.MyServlet</Servlet-class>    <Init-param>        <Param-name>User</Param-name>        <Param-value>Huey</Param-value>    </Init-param></servlet>

With Servlet3.0, you can also configure the initial parameters by @WebServlet:

@WebServlet (name = "Myservlet", Urlpatterns = {"/my"},         = {@WebInitParam (name= "user", Value= "Huey")} Publicclassextends  httpservlet {    //  ... }

Getinitparameter, Getinitparameternames and other methods can get the initial parameters:

 This . Getservletconfig (); Enumeration<String> initparamnames = servletconfig.getinitparameternames ();  while (Initparamnames.hasmoreelements ()) {    = (String) initparamnames.nextelement ();     + ":" + Servletconfig.getinitparameter (paramname));}

ServletContext

Servletcontent represents a servlet app, and each web app has only one context. In a distributed environment, where an application is deployed in multiple containers, each JVM has a ServletContext object. The ServletContext object can be obtained through the Servletconfig.getservletcontext () method. The following are common methods of the ServletContext class:

GetAttribute: Gets the properties of the app, and a similar method is getattributenames.

SetAttribute: Sets the properties of the app.

RemoveAttribute: Removes the app's properties.

Getcontextpath: Returns the context path of the Web application.

Getrealpath (String Path): Returns the absolute file path on the server file system for the given virtual path.

GetResource (String Path): Returns the URL to the resource mapped to the specified path. The path must begin with "/" and be interpreted relative to the current context root.

getResourceAsStream (String Path): Similar to GetResource, returns a InputStream object.

Servlet & Jsp-servlet API Overview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.