structurethe Java Web Engineering structure is as follows:
--web-inf|+--web.xml|+--classes|+--lib
where Web. XML is used to initialize the configuration information, classes holds the Java code in the project compiled into the class file, Lib contains references to the jar package. Let's look at the configuration of Web. Xml.
Filterfileter is defined as follows:
public interface filter{public void Init (Filterconfig filterconfig) throws Servletexception;public void DoFilter (Servle Trequest request, servletresponse response, Filterchain chain) throws IOException, Servletexception;public void Destroy ( );}
The configuration in Web. XML is as follows:
The function of the Characterencodingfilter above is to modify the encoding format in the response according to the configuration. The question now is when does the filter call? How to invoke? In fact, with four words can be summed up:
Stacked Call
with a filter of the following test, and then debug it to know:
public void DoFilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2) throws IOException, servletexception {S Ystem.err.println ("before"); Arg2.dofilter (arg0, arg1); System.err.println ("after");}
In addition, you need to use <filter-mapping> to configure the <filter> for the request.
ListenerThe configuration method is as follows:
<listener><listener-class>com.alibaba.citrus.webx.context.webxcontextloaderlistener</ Listener-class></listener>
The listener is used to listen for client requests, server operations, and so on, and defines the actions that should be taken at these times, with four target events:
- Servletcontextattributelistener: Depositing, deleting, and replacing attributes
- Changes in Servletrequestattributelistener:request properties
- Servletrequestlistener: Requested arrival, destruction.
- Changes in Httpsessionattributelistener:session properties
- Servletcontextlistener: Accepting changes in the life cycle of ServletContext
A collection of Web Parts that are installed in a particular URL namespace in the server makes up a web app that has contexts for each application, and ServletContext provides a way to access those contexts, getting servletcontext in the following way:
Webapplicationcontext WAC = Contextloader.getcurrentwebapplicationcontext (); ServletContext ServletContext = Wac.getservletcontext ();//TODO
Contextloaderlistener implements the Servletcontextlistener, which is called after the initialization of the ServletContext and completes the initialization of the spring context in the initialization method, as follows:
public void contextinitialized (Servletcontextevent event) {This.contextloader = Createcontextloader (); This.contextLoader.initWebApplicationContext (Event.getservletcontext ());}
The default path to find when initializing spring is /web-inf/applicationcontext.xml, which can be configured by parameter contextconfiglocation , such as:
<context-param><param-name>contextconfiglocation</param-name><param-value>classpath*: Cybertronspringconfig/spring-*.xml</param-value></context-param>
Context-paramThere are many places where you need to customize parameters, which you need to do with Context-param:
<context-param><param-name>log4jconfiglocation</param-name><param-value>/web-inf/ Log4j.xml</param-value></context-param>
You need to get it from ServletContext when you need it:
Servletcontext.getinitparameter (Config_location_param);
ServletThe servlet is used to process requests, and the correspondence of URLs needs to be configured in Web. xml:
<servlet><servlet-name>spring</servlet-name><servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class><load-on-startup>2</ load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name>< Url-pattern>/service/*</url-pattern></servlet-mapping>
in this way, you can use Dispatcherservlet to handle all /service/ requests. And Dispatcherservlet is a SPRINGMVC thing that distributes different requests to different handler processing. Where Load-on-startup is used to describe whether to load the servlet at boot time.