Java file notes _web.xml

  In JavaEE project, web.xml file is used to initialize the configuration information: such as the Welcome page, servlet, servlet-mapping, filter , listener, the boot loader level, etc. , but not required, not a Java Web project web.xml file It is still able to run up.

  When the web project started, the load order web.xml file of each node is: <context-param> -> <listener> -> <filter> -> <the servlet> .
Wherein, if the web.xml appears identical elements, in accordance with the order of appearance in the configuration file to load.

  web.xml schema defined in the file label is not married, the schema file also can be changed, generally speaking, with the version upgrade web.mxl pattern file, which defines the function will become increasingly complex, the label elements the species will surely be more and more, but some are not very common, we just remember some common and know how to configure it.

 

1, set the default home page access

1 <welcome-file-list>
2     <welcome-file>/pages/front/index.jsp</welcome-file>
3 </welcome-file-list>

 

2, the configuration servlet and servlet-mapping

Servlet: often referred to as a server-side applet, i.e. a program running on the server for processing and responding to customer requests .
Servlet is a special class java class inherits from HttpServlet.

. 1  < servlet > 
2      <-! Internal name of the servlet, custom, typically class name -> 
. 3      < servlet-name > ServletDemo1 </ servlet-name > 
. 4      <-! Must package name + class name. -> 
. 5      < the servlet-class > lm.package .ServletDemo1 </ the servlet-class > 
. 6  </ the servlet > 
. 7  
. 8  <-! to provide Servlet (map) for a client to access the URI -> 
. 9  < Mapping-servlet > 
10      <-! above the servlet servlet-name must be the same ->
11     <servlet-name>ServletDemo1</servlet-name>
12     <!-- servlet的映射路径 -->
13     <url-pattern>/ServletDemo1</url-pattern>
14 </servlet-mapping>

NOTE: The above servlet mapping path must be coupled / or an error occurs.

Map the path set above can be accessed through the address bar: http: // localhost / servlet

Supplementary 1: url-pattern:. * .Xxx * request to have access to a string Note: Do not add /
   url-pattern: / * any string can be accessed
   url-pattern: / xxx / * begin with / xxx of requests access to

 

Supplementary 2: servlet configuration action load-on-startup of

  If we set a number of servlet in web.xml, you can use the load-on-startup servlet to specify the load order of,

  The server will turn on servlet is initialized based on the size of the load-on-startup.

  But even if we will load-on-startup set to repeat will not appear abnormal, the server will decide initialization sequence.

  When the value is 0 or greater than 0, it represents the container when the application starts on the loading the servlet;

  When a negative number is or is not specified, it indicates that a container loaded when the servlet is selected.

  The smaller the value of a positive number, the higher priority start the servlet.

PS: Generally, we in the development of web applications, will configure this parameter has two advantages:

  1, if the initialization process fails, then the container will prompt fails to start, then we can know ahead of time-related errors;

  2, the configuration parameter corresponds to the initialization of the work will be transferred to the servlet container startup process, so long as the container is started successfully, the web request can respond immediately.

 

3, customize initialization parameters

  You can customize servlet, JSP, Context initialization parameters can then be re-servlet, JSP, Context acquire these parameter values.

 1 <servlet> 
 2     <servlet-name>ServletDemo1</servlet-name> 
 3     <servlet-class>Test.ServletDemo1</servlet-class> 
 4     <init-param> 
 5           <param-name>userName</param-name> 
 6           <param-value>Daniel</param-value> 
 7     </init-param> 
 8     <init-param> 
 9           <param-name>E-mail</param-name> 
10           <param-value>[email protected]</param-value> 
11     </init-param> 
12 </servlet> 

After the above configuration, the servlet can call getServletConfig (). GetInitParameter ( "param1") to obtain a value corresponding to the parameter name.

 

4, set the filter: Chinese distortion such as configuration processing filter class: CharacterEncodingFilter

 1 <filter>
 2     <filter-name>characterEncodingFilter</filter-name>
 3     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 4     <init-param>
 5         <param-name>encoding</param-name>
 6         <param-value>UTF-8</param-value>
 7     </init-param>
 8 </filter>
 9 
10 <filter-mapping>
11     <filter-name>characterEncodingFilter</filter-name>
12     <!--过滤所有-->
13     <url-pattern>/*</url-pattern>
14 </filter-mapping>

 

5, configure the Listener

Is actually a class, read applicationContext.xml file: ContextLoaderListener

1 <context-param>
2     <param-name>contextConfigLocation</param-name>
3     <param-value>classpath:applicationContext.xml</param-value>
4 </context-param>
5 <listener>
6     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
7 </listener>

 

6, session configuration (Session) expiration time, the time in minutes, 60 minutes timeout if provided

1 <session-config> 
2     <session-timeout>60</session-timeout> 
3 </session-config>

 

7, a page error handling, error handling may be specified by a page "error code" or "Exception Types"

 1 <error-page> 
 2     <!--错误码-->
 3     <error-code>404</error-code> 
 4     <location>/error404.jsp</location> 
 5 </error-page> 
 6 ----------------------------- 
 7 <error-page> 
 8     <!--异常类型-->
 9     <exception-type>java.lang.Exception<exception-type> 
10     <location>/exception.jsp<location> 
11 </error-page> 

 

8, configure log4j log

1 <context-param>
2     <param-name>log4jConfigLocation</param-name>
3     <param-value>classpath:properties/log4j.properties</param-value>
4 </context-param>
5 <listener>
6     <description>log4j配置加载器</description>
7     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
8 </listener>

 

9, the configuration springmvc core classes: DispatcherServlet, read springmvc.xml

 1 <servlet>
 2     <servlet-name>dispatcherServlet</servlet-name>
 3     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4     <init-param>
 5         <param-name>contextConfigLocation</param-name>
 6         <param-value>classpath:springmvc.xml</param-value>
 7     </init-param>
 8     <! - represents a start, a first start the servlet -> 
. 9      < Load-ON-Startup > . 1 </ Load-ON-Startup > 
10  </ servlet > 
. 11  < servlet-Mapping > 
12 is      < servlet -name > DispatcherServlet </ the servlet-name > 
13 is      < URL-pattern > * .do </ URL-pattern > 
14  </ the servlet-Mapping >

 

Guess you like

Origin www.cnblogs.com/one-smile/p/11765226.html