Java-Web. xml parsing and java-web. xml

Source: Internet
Author: User

Java-Web. xml parsing and java-web. xml


In Windows, IIS uses the UI interface for site configuration. in Linux, almost all systems use configuration files for configuration, java containers (JBoss/Tomcat/Jetty/WebSphere/WebLogic, etc.) are no exception. They use a WEB deployed under the web-INFO directory. xml is used as the site configuration file.

This article describes how to learn and record the loading sequence and configuration of web. xml by referring to Internet articles.


Web. xml Loading Sequence


When the application server is started. the xml loading process has nothing to do with the order of these nodes in the xml file. However, some application servers, such as WebSphere, strictly require the web. xml node order. Otherwise, the deployment will fail, so it is best to follow the web. standard xml format: context-param --> listener --> filter --> servlet.

For certain configuration sections, the order in which they appear is related.

Take filter as an example. of course, multiple filters can be defined in xml. One configuration section related to the filter is filter-mapping. Note that, in the filter and filter-mapping configuration sections with the same filter-name, the filter-mapping must appear after the filter; otherwise, when the filter-mapping is parsed, the filter-name corresponding to it is not defined yet.

When a web container is started, each filter is initialized according to the sequence in the filter configuration section. When the requested resource matches multiple Filters-mapping, filter intercepts resources by calling the doFilter () method in sequence in the filter-mapping configuration section.

Servlet is similar to filter. We will not repeat it here.

For example, if the filter requires bean, but the loading order is: load the filter first and then load spring, the bean in the initialization operation of the filter is null. Therefore, if the bean is used in the filter, you can change spring loading to Listener:

 <listener>     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>


Web. xml node Parsing
<Context-param/>


Used to set the environment parameters of a web site

It contains two sub-elements: <param-name> </param-name> is used to specify the parameter name; <param-value> </param-value> is used to set the parameter value.

The parameter set here can be obtained using getServletContext (). getInitParameter ("my_param") in servlet.

Example:

<context-param><param-name>webAppRootKey</param-name><param-value>privilege.root</param-value></context-param><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/applicationContext*.xmlclasspath*:/cas-authority.xml</param-value></context-param><context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/classes/log4j.xml</param-value></context-param><context-param>  <param-name>spring.profiles.default</param-name>  <param-value>dev</param-value>  </context-param> 


<Listener/>


Used to set the Listener Interface

Its main sub-element is <listener-class> </listener-class>, which defines the class name of Listener.

Example:

 <listener>     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

<Filter/>


Used to declare filter settings

<Filter-name> </filter-name> specifies the filter name.

<Filter-class> </filter-class> defines the name of the filter class.

<Init-param> </init-param> is used to define a parameter. It has two sub-elements: <param-name> </param-name> to specify the parameter name, <param-value> </param-value> is used to set the parameter value.

Which is used with <filter> </filter>?

<Filter-mapping> </filter-mapping> defines the URL of the filter, which contains two child elements:

<Filter-name> </filter-name> specifies the filter name.

<Url-pattern> </url-pattern> specifies the URL of the filter.

Example:

<! -- Solve Chinese garbled characters --> <filter-name> encodingFilter </filter-name> <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init- param> <init-param> <param-name> forceEncoding </param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> encodingFilter </filter-name> <url-pattern> *. do </url-pattern> </filter-mapping>


<Servlet/>


The data used to declare a servlet mainly includes the following sub-elements:

<Servlet-name> </servlet-name> specifies the servlet name.

<Servlet-class> </servlet-class> specifies the servlet class name.

<Jsp-file> </jsp-file> specifies the complete path of a JSP page on the web platform.

<Init-param> </init-param> is used to define parameters.

Which is used together with <servlet> </servlet>?

<Servlet-mapping> </servlet-mapping> defines the URL of the servlet, which contains two child elements:

<Servlet-name> </servlet-name> specifies the servlet name.

<Url-pattern> </url-pattern> specifies the URL of the servlet.

Example:

<Servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> springmvc </ servlet-name> <url-pattern> *. do </url-pattern> </servlet-mapping> <! -- Use dubbo to provide hessian services --> <servlet-name> dubbo </servlet-name> <servlet-class> com. alibaba. dubbo. remoting. http. servlet. dispatcherServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> dubbo </ servlet-name> <url-pattern>/hessian/* </url-pattern> </servlet-mapping>


Basic Node


1. <description/> is the description of the site.

Example: <description> preaching, teaching, and confusing </description>


2. <display-name/> define the site name

Example: <display-name> my site </display-name>


3. <icon>

The icon element contains two sub-elements: small-icon and large-icon, which are used to specify the paths of small icons and large icons on the web site.

<Small-icon>/path/smallicon.gif </small-icon>

The small-icon element should point to the path of a small icon in the web platform. The size is 16X16 pixel, but the image file must be in GIF or JPEG format. The extension must be .gif or .jpg.

<Large-icon>/path/largeicon-jpg </large-icon>

The large-icon element should point to a large chart path on the web platform. The size is 32X32 pixel, but the image file must be in GIF or JPEG format. The extension must be gif or jpg.

Example:

<Icon>

<Small-icon>/images/small.gif </small-icon>

<Large-icon>/images/large. gir </large-icon>

</Icon>


4. <distributable/> specifies whether the site can be processed in a distributed manner.


5. <session-config/> is used to define the session parameters in the web platform.

Contains a child element:

<Session-timeout> </session-timeout> defines the validity period of all sessions on the web Platform, measured in minutes.


6. <mime-mapping/> defines an extension that corresponds to a MIME Type. It contains two child elements:

<Extension> </extension> name of the extension

<Mime-type> </mime-type> MIME format

Example:

<mime-mapping><extension>csv</extension><mime-type>application/octet-stream</mime-type></mime-mapping>

7. <error-page>

Configure error-page through error codes

 <error-page>     <error-code>404</error-code>     <location>/message.jsp</location> </error-page>

Configure error-page through exception classes

   <error-page>           <exception-type>java.lang.NullException</exception-type>           <location>/error.jsp</location>       </error-page>  

8. <welcome-file-list/>

<welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.jsp</welcome-file></welcome-file-list>

9. <resource-ref> </resource-ref> defines the resources available for the platform through JNDI.

There are five child elements:

<Description> </description> Resource description

<Rec-ref-name> </rec-ref-name> Resource name

<Res-type> </res-type> Resource type

<Res-auth> </res-auth> resources are licensed by Application or Container.

<Res-sharing-scope> </res-sharing-scope> whether resources can be shared. There are two values: retriable and unretriable. The default value is retriable.

For example, you can configure the database connection pool here.

<Resource-ref>

<Description> jndi jdbc DataSource of shop </description>

<Res-ref-name> jdbc/sample_db </res-ref-name>

<Res-type> javax. SQL. DataSource </res-type>

<Res-auth> Container </res-auth>

</Resource-ref>


How to use java in a webxml File

If you want to retrieve all the values, we recommend that you parse WEB. XML and then set the values. If you want to retrieve a specific value, you can use the following:
Two parameters can be defined in web. xml:
(1) parameters within the application range are stored in servletcontext and configured in web. xml as follows: xml Code
<Context-param>
<Param-name> context/param </param-name>
<Param-value> avalible during application </param-value>
</Context-param> (2) servlet-specific parameters can only be obtained in the init () method of servlet. In web. xml, configure the following: xml Code
<Servlet>
<Servlet-name> MainServlet </servlet-name>
<Servlet-class> com. wes. controller. MainServlet </servlet-class>
<Init-param>
<Param-name> param1 </param-name>
<Param-value> avalible in servlet init () </param-value>
</Init-param>
<Load-on-startup> 0 </load-on-startup>
</Servlet> you can use the java code in the servlet separately.
Package com. test;

Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;

Public class TestServlet extends HttpServlet ...{

Public TestServlet ()...{
Super ();
}
Public void init () throws ServletException ...{
System. out. println ("the following two parameters param1 are stored in servlet ");
System. out. println (this. getInitParameter ("param1 "));
System. out. println ("the following parameters are stored in servletcontext ");
System. out. println (getServletContext (). getInitParameter ("context/param "));
} ...... Remaining full text>

Java webxml

1. when starting a WEB project, the container (such as Tomcat) will read its configuration file web. xml. read two nodes: <listener> </listener> and <context-param> </context-param>
2. Then, the container creates a ServletContext, and all parts of the WEB project share the context.
3. The Container converts <context-param> </context-param> to a key-Value Pair and submits it to ServletContext.
4. Create a class instance in the <listener> </listener> container, that is, create a listener.
5. the contextInitialized (ServletContextEvent args) initialization method is available in the listener. In this method, the ServletContext = ServletContextEvent is obtained. getServletContext (); Value of context-param = ServletContext. getInitParameter ("context-param key ");
6. after obtaining the value of context-param, you can perform some operations. note that your WEB project has not been fully started yet. this action will be earlier than all servlets. in other words, the operations you perform on the key value in <context-param> will be executed before your WEB project is fully started.
7. example. you may want to open the database before the project starts. then, you can set the database connection method in <context-param> and initialize the database connection in the listener class.
8. This listener is a self-written

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.