This is the 15th day of my participation in Gwen Challenge

Today, AH Q will show you what a servlet is.

Introduction of the Servlet

Servlets are Java applets running on the server. They are a set of specifications (interfaces) provided by Sun Corporation, which are used to process dynamic resources that the client requests and responds to the browser. But the essence of servlets is Java code that dynamically outputs content to clients through Java apis.

Servlet specification: Contains three technical points

1) Servlet technology

2) Filter technology — filter

3) Listener technology – Listener

Servlet Quickstart

Implementation steps:

1) Create classes that inherit from the HttpServlet class

2) Override doGet and doPost methods

3) Configure the servlet in web.xml

Servlet apis (Lifecycle)

Methods in the Servlet interface

1) init (ServletConfig config)

When: Executed when the servlet object is created

ServletConfig: represents the configuration information for this servlet object

2) Service (ServletRequest Request,ServletResponse response)

When to execute: Each request is executed

ServletRequest: Represents a request that encapsulates the information contained in the HTTP request

ServletResponse: Represents the response and considers the information of the response to be encapsulated

3) destroy ()

When: When the servlet is destroyed

HttpServlet class method

1) the init ()

2) the doGet (it request, HttpServletResponse response)

3) the doPost (it request, HttpServletResponse response)

4) destroy ()

The life cycle of a Servlet

1) When the Servlet is created

This object is created by default the first time the servlet is accessed

2) When the Servlet is destroyed

The servlet is destroyed when the server shuts down

3) Methods that must be executed on each access

Service (ServletRequest req, ServletResponse Res) method

Servlet access process:

The Servlet configuration

The basic configuration



The url-pattern configuration mode is as follows:

1) Full match: Access only when the accessed resources are the same as configured resources

2) Directory matching format: / virtual directory… /* * stands for any

3) Extension matching format: *. Extension

/aaa/ BBB /*.abcd (incorrect)

The server starts instantiating the Servlet configuration

When a Servlet is created: By default, it is created on first access. Why default? The configuration servlet object is created when the server starts when a configuration servlet is added to the configuration of the servlet.

The default Servlet

The url-pattern can be configured with a slash to indicate that the servlet is the default servlet. What is the default servlet? When you access a resource address and all servlets do not match, the default servlet is responsible for handling it. In fact, all resource responses in a Web application are handled by servlets, including static resources.

ServletContext object

ServletContext object

A ServletContext represents the context object of a Web application. The internal encapsulation of a ServletContext object is the information of the Web application. A ServletContext object has only one web application.

The life cycle of the ServletContext object?

Create: The Web application is loaded (the server starts or publishes the Web application (prerequisite, the server is started))

Destroy: The Web application is uninstalled (the server is shut down, the Web application is removed)

How do I get the ServletContext object

1) ServletContext ServletContext = config.getServletContext();

2) ServletContext ServletContext = this.getServletContext();

The role of the ServletContext

(1) Obtain the global initialization parameters of the Web application:

Configure initialization parameters in web.xml

Get the parameters from the context object

(2) The absolute path to any resource in the Web application ** (important important important) **

Method: String path = context.getRealPath(relative to the address of the Web application);

ServletContext is a domain object ** (important important important) **

The area where the data is stored is the domain object. The scope of the ServletContext domain object: The entire Web should (all Web resources can freely access data from the ServletContext domain, data can be shared)

Common methods for domain objects:

setAtrribute(String name,Object obj);

getAttribute(String name);

removeAttribute(String name);

That’s all for today’s content. If you have different opinions or better ideas, please contact Ah Q: Qingqing-4132. Ah Q can invite you to join the technical group to discuss technology together. Ah Q is looking forward to your arrival!