A common text to understand web.xml configuration items SSM project

Do the back-end web engineer, can not escape the web.xml, we all know that this configuration file is the daily work, then to our knowledge must know some sort.

    We started the web project, the first thing is to load the web.xml file, the file only after all the configuration information Yes, web project one truly up and running.

    Then we open the door with configuration information web.xml it.

 

First, we need to be sure one is, regardless of the configuration sequence web.xml which is kind of how the information as a whole, our execution order is not executed according to your configuration file written in the order, have their own web.xml execution order

 

Initialization process:

  1. When starting Web items, containers (such as Tomcat) will read two nodes <listener> and <contex-param> web.xml configuration file.
  2. Then the container creates a ServletContext (context), that can be used throughout the WEB project within the context of this application.
  3. Then the container will be read into the <context-param> into key-value pairs and to ServletContext.
  4. Create a container class instance <listener> </ listener>, that is create a listener (Note: listener classes can be defined in a custom class needs to inherit but must ServletContextListener).
  5. Listening class have a contextInitialized (ServletContextEvent event) initialization method, in this method may be a value obtained by the context-param set event.getServletContext (). GetInitParameter ( "contextConfigLocation"). There must also be in this class a contextDestroyed (ServletContextEvent event) destruction methods. For releasing resources before closing the application, for example, close the database connection.
  6. After obtaining the value of context-param, you can do some operate. Note that this time your WEB project has not been fully completed to start. This action will be earlier than all of the Servlet.

Is performed under the same sequence is performed in accordance with types of the mapping information corresponding to

    In accordance with the order of execution xml, let's look at each in turn execute the order of the label is what is meant

Worth noting that, web project at startup, container (Tomcat) creates a domain object ServletContext, we briefly explain

ServletContext

The official explanation is that the servlet context, when activated, will give each server a project to create an object that is ServletContext object, and the object is a globally unique. And the whole project within the servlet share this object.

First, let's remember, ServletContext is a domain object, do not rush, which gives you tell us about the domain objects

 

 

By the figure, I believe you can understand in this domain objects are to act as what role

    So exactly what is it the domain object

    Storage server domain object is created in memory for the transfer and sharing of resources between different dynamic (the servlet) Data

Any domain object has the following three methods

SetAttribute(name,value)

Adding data to the field inside the object, is added in the form of key-value added time

GetAttribute(name);

Reading data inside the object domain according to the specified key

RemoveAttribute(name)

Delete data from the inside of the object with the specified key field

I believe that here, played a lot of children's shoes have learned the code is valid and whether the domain object thing, SetAttribute these methods, when we pass data to the domain object, is frequently used.

 

So then we can come to find out

context-param 

Action: This element is used within the context initialization parameters declared application (the entire WEB item).

Param-name in there with param-value

param-name setting parameter name context. The name must be unique

param-value parameter setting name

After the reading is completed, we are transforming this information into key-value pairs, handed over ServletContext

 

According to the order, we need to know that next Listener

 

Listener translates to the listener, as the name suggests is used to monitor our web activity, this is well understood, servlet provided a total of eight listeners

 

Three domain objects are created and destroyed listeners

Object Types

Corresponding listener

ServletContext

ServletContextListener

HttpSession

HttpSessionListener

HttpServletRequest

HttpServletRequestListener

 

Listener object property changes three listener ( property to add, remove property, property replacement )

Object Types

Corresponding listener

ServletContext

ServletContextAttributeListener

HttpServletRequest

ServletRequestAttributeListener

HttpSession

HttpSessionAttributeListener

 

Monitor the change of state JavaBean HttpSession object (binding, unbinding the passivation and activation)

 

Object Types

Corresponding listener

HttpSession

HttpSessionBindingListener (bind, unbind)

HttpSession

HttpSessionActivationListener (passivation and activation)

 

About then listen to it here, in-depth content I will write a blog in addition to our in-depth introduction

 

Next is about the filter , that is, filter, filter by definition is used to filter some of the information needed

 

filter

Data on the web filter is actually submitted over, the information processed by the server, re-delivered to the other party. You can be understood as a place of intermediate processing.

    They are often used to intercept request, and response intercept filtering

通过上图,我们可以更直观地了解到过滤器的工作角色,值得我们注意的是,过滤器可能会有多个,经过过滤器一过滤完之后,还需要通过滤器二进行过滤处理,因为每个过滤器都有自己特定的过滤功能

 

关于多个过滤器的执行顺序,我们请求到达Servlet之间是可以经过多个过滤器的,这些过滤器的执行顺序是依赖于<filter-mapping>里面配置的顺序。

 

servlet 

servlet全称Java Servlet,是用Java编写的服务端程序,其主要功能在于交互式地浏览和修改数据,生成动态Web内容。

在web.xml这里主要是将你所写的servlet对应的类配置到当中去,具体的servlet的详解可以看我关于servlet详解的文章

Guess you like

Origin www.cnblogs.com/waibizi/p/11330046.html