Servlet execution process life cycle servletconfig thread safety

Source: Internet
Author: User

Day34 servlet

third, how to use Servlet

1, Inherit the Genericservlet class (general)

(1) The Genericservlet class has a key design that defines a private ServletConfig member variable that, in the Init () method, is matched by an incoming ServletConfig object from the servlet container.

(2) The only abstract method in the Genericservlet class is the service method, and the subclass must implement the service method to provide the user with specific services.

2. Inherits the HttpServlet class, is the subclass of the Genericservlet, overrides the service method, and provides the related service implementation of HTTP. The user needs to do, according to the actual situation, rewrite dopost (), doget () and other methods.

 

Servlet is One of the three components of Javaweb (servlet,Filter, Listener), and the most important

1.1.1 S Ervlet Getting Started ( Focus )

mode 1: implement the Sevrlet interface ( learn )

Mode 2:

To write a servlet step :

servlet Specification Requirements:servlet programs need to write implementation classes and Configure them in Web. xml

L inherit Javax.servlet.http.HttpServlet

L Carbon doget () and dopos () T method

configuring the Web. XML File

1.1.1.1 A detailed description of the Servlet 's execution process

1.1.1.2 R equest GET request Parameters ( Master )1.1.2 Request receive parameter 1.1.2.1 request parameters for receiving requests Overview

Request parameters

Method name

Describe

String GetParameter (string name)

Gets the value that corresponds to the specified parameter name. If nullis not returned, if only more than one gets the first one.

Example:username=jack

1.1 S Ervlet life cycle ( Focus )

L Concept : The servlet lifecycle is the process of creating a Servlet object from creation to destruction

n when to create : The first time a user accesses A servlet is to create

N when to destroy : The project is removed from the server or the server is stopped

The servlet stipulates that all servlets must implement Javax.servle. Servlet Interface

l void Init (servletconfig config): initialization Method

* Initialization Method

* Execution Time : default to First visit

* execution times : only once in a lifetime ( singleton )

* performer : server (tomcat)

l void Service (ServletRequest request,servletresponse response): Service method

* Service method

* Execution time : every visit

* execution times : access is performed once

* performer : Server

l void Destroy (): Method of Destruction

* method of Destruction

* Execution Time : When the project is removed or when the server shuts down properly

* execution times : only once in a lifetime

* performer : Server

1.2 servletconfig Introduction ( Understand )

L Concept

n servlet Configuration object , one servlet corresponding to a servlet Configuration Object

L Effect

N get the servlet name <servlet-name>ConfigServlet</servlet-name>

n get The initialization parameters of the servlet

n Get Context object ( Global manager ServletContext)

L Creation (ServletConfig life cycle ):

while the server creates the servlet , It also creates a configuration object for the current servlet. passed to the current servlet through the servlet 's init method

gets the ServletConfig.

the servlet passed the getservletconfig ()

Common APIs (learn)

Method name

return value type

Describe

Getinitparameter (name)

String

Gets the initialization parameters of the Servlet .

Getinitparameternames ()

Enumeration

Gets the name of the initialization parameter for all Servlets

Getservletcontext ()

ServletContext

Get a reference to a ServletContext object

Getservletname ()

String

Gets The value of the name that the Servlet configures in Web. XML .

and <servlet-name> tag configuration content.

, how to ensure servlet Thread Safety

1, to ensure that the variable scope is reasonable, thread-private variables to be defined in the method.

2, shared variables to ensure thread safety, you can use locking, atomic class, volatile keyword implementation.

Servlet execution process life cycle servletconfig thread safety

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.