Java Web Engineering web.xml configuration detailed __web

Source: Internet
Author: User
Tags auth tld java web

In Java Engineering, Web.xml is used to initialize engineering configuration information, such as welcome pages, filter,listener,servlet,servlet-mapping, starting load levels, and so on.

Each XML file has a schema file that defines his writing specification, and how many tag elements are defined in the corresponding XML schema file, and what specific functionality is available in the Web.xml as it web.xml the label elements it defines. Web.xml's schema file is defined by sun, and the root element of each web.xml file is <web-app> and must indicate which schema file the Web.xml is using.

The root element of the web.xml is defined as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version=
"2.5" xmlns= "http://java.sun.com/xml/ns/" 
Java ee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " 
xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
</web-app>

Here's a look at the tags and their functions that are commonly used in Web.xml

1.<description>,<display-name>,<icon>

<description> Project Description </discription> description of the project.

<display-name> Project name </display-name> define the name of the project.

The <icon> icon element contains Small-icon and Large-icon two child elements. Used to specify the path of small and medium icons and large icons on the Web site.

The <small-icon>/path/smallicon.gif</small-icon>small-icon element should point to the path of a small icon in the Web site, in size X pixel, However, the image file must be in GIF or JPEG format, and the extension must be:. gif or. jpg.

The <large-icon>/path/largeicon-jpg</large-icon> large-icon element should point to a large chart path in the Web site, size X pixel, But the image file must be in GIF or JPEG format, and the extension must be; GIF or JPG.

For example:

<display-name>develop example</display-name>  
<description>jsp 2.0 Tech book ' s Examples</ description> 
 <icon>  
   <small-icon>/images/small.gif</small-icon>    
   <large-icon >/images/large.gir</large-icon> 
 </icon>

2.<context-param>The <context-param> element contains a pair of parameter names and argument values that are used as servlet context initialization parameters for the application. Parameter names must be unique throughout the Web application.
The Context-param element is used to set the environment parameter (context) of the Web application, which contains two child elements: Param-name and Param-value.
<param-name> parameter name </param-name> Set context Name
<param-value> value </param-value> Setting the value of the context name </context-param>
For example:
<context-param>
   <param-name>param_name</param-name>
   <param-value>param_value </param-value>
</context-param>
The parameters that are set in the JSP Web page can be obtained by using the following methods: ${initparam.param_name}
In the servlet, you can use the following methods to obtain:
String Param_name=getservletcontext (). Getinitparamter ("Param_name");
3.<filter>

The filter element is used to specify a filter in the Web container.

You can use a filter to manipulate both objects before or after the request and response objects are processed by the servlet.

Using the filter-mapping element described in the next section, the filter is mapped to a servlet or a URL pattern.

The filter element and the filter-mapping element must have the same name. The filter element is used to declare the relative settings of the filter. The filter element includes the <icon>,<display-name>,<description> described earlier, in addition to the child elements described below;     <init-param>, for the same purpose. The following describes the Filter-name,filter-class and Init-param elements
The Init-param element has the same element descriptor as the Context-param element.         The Filter-name element is used to define the name of the filter, which must be unique throughout the application. The Filter-class element specifies the fully qualified name of the filter class.


The name of the <filter-name>filter </filter-name> defines the name of the filter.
<filter-class>filter class name </filter-class> defines the class name for filter. For example: Com.foo.hello for example:

<filter>
  <filter-name>setCharacterEncoding</filter-name>
  <filter-class> coreservlet.javaworld.ch11.setcharacterencodingfilter</filter-class>
  <init-param>
     < param-name>encoding</param-name>
     <param-value>GB2312</param-value>
  </ Init-param>
</filter>
4.<filter-mapping>

The filter-mapping element is used to declare filter mappings in Web applications. Filters can be mapped to a servlet or a URL pattern. Mapping a filter to a servlet causes the filter to function on the servlet. Mapping a filter to a URL pattern allows you to apply a filter to any resource, as long as the URL of the resource matches the URL pattern. Filtering is performed in the order in which the filter-mapping elements of the deployment descriptor appear.

The two primary child elements of the filter-mapping element Filter-name and Url-pattern. Defines the URL for the filter. There are also servlet-name and dispatcher elements that are not commonly used.

The name of the <filter-name>filter </filter-name> defines the name of the filter.

<url-pattern>URL</url-pattern> Filter corresponding to the Rul. For example: <url-pattern>/filter/hello</url-pattern >
<servlet-name>servlet name <servlet-name> defines the name of the servlet.
<dispatcher>request| include| forward| Error</disaptcher> set the filter corresponding request mode, there are rquest,include,forwar,error four kinds, default for request.

For example:

<filter-mapping>
   <filter-name>GZIPEncoding</filter-name>
   <url-pattern>/*</ Url-pattern>
</filter-mapping>

The complete filter configuration example is as follows:

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</ Url-pattern>
	</filter-mapping>

5.<servlet>

One of the most common tasks accomplished in Web.xml is to give a name and a custom URL to a servlet or JSP page. Assigns a name with a servlet element, using the servlet-mapping element to associate the custom URL with the name you just assigned. For example:

<servlet> 
  <servlet-name>Test</servlet-name> 
  <servlet-class> Com.moreservlets.testservlet</servlet-class> 
This means that the servlet in Web-inf/classes/com/moreservlets/testservlet has been registered with the name test. 6.<servlet-mapping>The servlet-mapping element contains two child elements servlet-name and url-pattern. Used to define the corresponding URL for the servlet.
<servlet-name>servlet name </servlet-name> defines the name of the servlet.
<url-pattern>servlet Url</url-pattern> defines the rul that the Servlet corresponds to. For example: <url-pattern>/servlet/hello</ Url-pattern> For example:
<servlet-mapping>
   <servlet-name>LoginChecker</servlet-name>
   <url-pattern>/ Loginchecker</url-pattern>
</servlet-mapping>
The full servlet configuration looks like this:
<servlet>
  <servlet-name>ServletName</servlet-name>   
  <servlet-class> Xxxpackage.xxxservlet</servlet-class>   <!--servlet class-->
  <init-param>                                     <!-- Initializes a variable that can be viewed as a global variable and can be omitted-->
    <param-name> parameter name </param-name>              <!--variable name-->
    < param-value> parameter value </param-value>              <!--variable value-->
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>ServletName</servlet-name>               
  <url-pattern >/aaa/xxx</url-pattern>                   <!--map URL path-->
</servlet-mapping>
Enter HTTP://LOCALHOST:8080/WEB-APP/AAA/XXX in the address bar to access it. 7.<listener>The listener element is used to register a listener class that can be included in a Web application. Using the listener element, you can receive notification of when an event occurred and in what response.
The listener element is used to define the listener interface, and its primary child element is <listener-class>
<listen-class>listener class name </listener-class> Define listener class name. For example: Com.foo.hello
For example:
<listener>
    <listener-class>com.foo.hello</listener-class>
</listener>
8.<session-cofing>Session-config contains a child element Session-timeout. Defines the session parameters in a Web application.
<session-timeout> min </session-timeout> defines the expiration date for all sessions of this Web application. The unit is minutes.
For example:
<session-config>
   <session-timeout>20</session-timeout>
</session-config>
9.<mime-mapping>Mime-mapping contains two child elements extension and Mime-type. Defines an extension and a MIME type to do the mapping.
<extension> extension name </extension> extension name
<mime-type>mime format </mime-type> MIME format.
For example:
<mime-mapping>
   <extension>doc</extension>
   <mime-type>application/vnd.ms-word </mime-type>
</mime-mapping>
<mime-mapping>
   <extension>xls</extension >
   <mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
< mime-mapping>
   <extension>ppt</extesnion>
   <mime-type>application/vnd.ms-powerpoint </mime-type>
</mime-mapping>
10.<welcome-file-list>Welcome-file-list contains a child element welcome-file. Used to define the first list.
<welcome-file> to specify the first page file name </welcome-flie>
Welcome-file is used to specify the first page file name. We can use <welcome-file> to specify a few home pages, and the server will be in accordance with the order to find the home page.
For example:
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file> Index.htm</welcome-file>
</welcome-file-list>
One .<error-page> The Error-page element contains three child elements error-code,exception-type and location. The type of error code or exception (Exception) is mapped to the Web application resource path.
<error-code> Error Codes </error-code> HTTP error code, for example: 404
<exception-type>Exception</exception-type> Java exception type with a full name
<location>/path </location> related resource paths within Web applications
For example:
<error-page>
   <error-code>404</error-code>
   <location>/error404.jsp</location >
</error-page>
<error-page>
   <exception-type>java.lang.exception</ exception-type>
   <location>/except.jsp</location>
</error-page>
<jsp-config> The Jsp-config element is primarily used to set the related configuration of the JSP,<jsp:config> including <taglib> and <jsp-property-group> two child elements. of which <taglib> The element already exists at JSP 1.2, and <jsp-property-group> is the new element of JSP 2.0.

<taglib>
The Taglib element contains two child elements Taglib-uri and taglib-location. Used to set the tag library path used for JSP pages.
<taglib-uri>URI</taglib-uri> Taglib-uri The taglib instructions for uri,jsp Web pages that define TLD files can be accessed via this URI to the TLD file.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction> TLD file corresponds to where the WEB application is stored.

<jsp-property-group>
The Jsp-property-group element contains 8 elements, respectively:
<description>Description</descrition> Description of this setting
<display-name>Name</display-name> Name of this setting
<url-pattern>URL</url-pattern> the range of values affected, such as:/ch2 or/*.jsp
<el-ignored>true|false</el-ignored> If True, indicates that El syntax is not supported.
<scripting-invalid>true|false</scripting-invalid> If True indicates that <%scription%> syntax is not supported.
<page-encoding>encoding</page-encoding> set the code for JSP pages
<include-prelude>.jspf</include-prelude> set the head of the JSP page with the extension. JSPF
<include-coda>.jspf</include-coda> set the end of the JSP page with the extension. JSPF
For example:
<jsp-config>
    <taglib>
        <taglib-uri>Taglib</taglib-uri>
        <taglib-location >/WEB-INF/tlds/MyTaglib.tld</taglib-location>
    </taglib>
    <jsp-property-group>
        <description>
          Special Property Group for JSP Configuration JSP example.
        </description>
        <display-name>JSPConfiguration</display-name>
        <uri-pattern>/* </uri-pattern>
        <el-ignored>true</el-ignored>
        <page-encoding>gb2312</ page-encoding>
        <scripting-inivalid>true</scripting-inivalid>
    ....... </jsp-property-group>
</jsp-config>
12.<resource-ref>The Resource-ref element includes five child element Description,res-ref-name,res-type,res-auth,res-sharing-scope. Use Jndi to obtain application available resources. <description> Description </description> Resource Description
<rec-ref-name> Resource Name </rec-ref-name> resource Name
<res-type> resource Types </res-type> resource types
<res-auth>application| Container</res-auth> resources are licensed by application or Container.
<res-sharing-scope>shareable| Unshareable</res-sharing-scope> whether the resource can be shared. The default value is shareable for example:
<resource-ref>
   <description>jndi JDBC DataSource of jspbook</description>
   < Res-ref-name>jdbc/sample_db</res-ref-name>
   <res-type>javax.sql.DataSoruce</res-type>
   <res-auth>Container</res-auth>
</resource-ref>



























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.