Basic configuration of web.xml in JavaWeb project

Basic configuration of web.xml in JavaWeb project

1. Theoretical preparation

    先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对。

Is web.xml necessary? No, it's fine as long as you don't need the configuration information in it, but it's very convenient to use this file in a large web project, otherwise it would be very complicated.

So what are all the things web.xml can do? In fact, no matter how many kinds of label elements are defined in the schema file of web.xml, the label elements defined by its schema file can appear in web.xml, and it can have the functions defined. The schema file of web.xml is defined by Sun. The root element of each web.xml file must indicate which schema file the web.xml uses.

Let's see an example:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>db</display-name>

  <welcome-file-list>

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

    <welcome-file>index.htm</welcome-file>

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

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

2. Label elements

指定欢迎页面

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

<welcome-file>index1.jsp</welcome-file>

    上面的例子指定了2个欢迎页面,显示时按顺序从第一个找起,如果第一个存在,就显示第一个,后面的不起作用。如果第一个不存在,就找第二个,以此类推。
    关于欢迎页面:访问一个网站时,默认看到的第一个页面就叫欢迎页,一般情况下是由首页来充当欢迎页的。一般情况下,我们会在web.xml中指定欢迎页。但web.xml并不是一个Web的必要文件,没有web.xml,网站仍然是可以正常工作的。只不过网站的功能复杂起来后,web.xml的确有非常大用处,所以,默认创建的动态web工程在WEB-INF文件夹下面都有一个web.xml文件。
    对于tomcat来说,当你只指定一个web的根名,没有指定具体页面,去访问时一个web时,如果web.xml文件中配置了欢迎页,那么就返回指定的那个页面作为欢迎页,而在文中没有web.xml文件,或虽然有web.xml,但web.xml也没指定欢迎页的情况下,它默认先查找index.html文件,如果找到了,就把index.html作为欢迎页还回给浏览器。如果没找到index.html,tomcat就去找index.jsp。找到index.jsp就把它作为欢迎页面返回。而如果index.html和index.jsp都没找到,又没有用web.xml文件指定欢迎页面,那此时tomcat就不知道该返回哪个文件了,它就显示The requested resource (/XXX) is not available(我就出现过这个问题)的页面。其中XXX表示web的根名。但如果你指定了具体页面,是可以正常访问的。

命名与定制URL
<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>net.test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
    url-pattern的意思是所有的.do文件都会经过TestServlet处理。

定制初始化参数
<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>net.test.TestServlet</servlet-class>
    <init-param>
          <param-name>userName</param-name>
          <param-value>Tommy</param-value>
    </init-param>
    <init-param>
          <param-name>E-mail</param-name>
          <param-value>[email protected]</param-value>
    </init-param>

</servlet>
    经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("param1")获得参数名对应的值。

//Context parameters: declare the initialization parameters in the application scope.

<context-param>  
    <param-name>ContextParameter</para-name>  
    <param-value>test</param-value>  
    <description>It is a test parameter.</description>  
</context-param>  

//In the servlet, you can pass getServletContext().getInitParameter("context/param")

get

指定错误处理页面,可以通过“异常类型”或“错误码”来指定错误处理页面。
<error-page>
    <error-code>404</error-code>
    <location>/error404.jsp</location>
</error-page>
----------------------------
<error-page>
    <exception-type>java.lang.Exception<exception-type>
    <location>/exception.jsp<location>
</error-page>
<error-page>  
      <exception-type>java.lang.NullException</exception-type>  
      <location>/error.jsp</location>  
</error-page> 
设置过滤器:比如设置一个编码过滤器,过滤所有资源
<filter>
    <filter-name>XXXCharaSetFilter</filter-name>
    <filter-class>net.test.CharSetFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>XXXCharaSetFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

6. Set up the listener

What is the use of web.xml? No other use! It is to configure the monitoring class~, which can capture the start and stop of the server! The methods in the start and stop triggers do the corresponding operations! It must be in web.xml It can only be used in the configuration in the web.xml! There can only be one listener element in web.xml, and if there are multiple ones, they will be executed in order.

How to pass parameters to listener in web.xml?

<listener> 
     <listener-class>监听器类的完整路径</listener-class> 
</listener>

The initialization parameters cannot be written in the listener; the effect of the initialization parameters can be achieved through another way: 1. Write a properties file, and write the initialization parameter values ​​in the file, 2. In the listener, you can get the properties in the file. value (written in a static block).

设置会话(Session)过期时间,其中时间以分钟为单位
<session-config>
     <session-timeout>60</session-timeout>
</session-config>

In addition to these tag elements, you can add those tag elements to web.xml, and what role can those tag elements play? We just need to look at the schema file of web.xml to find out. If you can't read the pattern file directly, you can find some Chinese tutorials to take a look.

3. Remaining issues

There are always some issues about loading priorities in the project, and I have encountered similar issues recently, so I found the information and summarized it myself. Some of the following are reprinted by other people. The wheels are repeated, just a little bit of my own modification. The first thing to be sure is that the load order has nothing to do with their order in the web.xml file. That is, the filter will not be loaded first because the filter is written in front of the listener. The final conclusion is: listener -> filter -> servlet. If a session is not accessed for a certain period of time, the server can discard it to save memory. The timeout value for a single session object can be set explicitly by using the setMaxInactiveInterval method of HttpSession, or a default timeout value can be specified using the session-config element.
Instructs the server which file to use when it receives a URL that refers to a directory name instead of a file name.
write picture description here
Then access the servlet in the application directory.
Since there is url-pattern in web.xml, why do we need servlet-class? url-pattern indicates what format the url enters into the servlet, and servlet-class indicates which java class handles the request. When a request comes, first go to servlet-mapping to find the url corresponding to the url according to the requested url- pattern, find the servlet-name after finding the url-pattern, and then find the corresponding servlet-class according to the servlet-name and the servlet-name in the servlet, and the specific business logic is written in the java class corresponding to the servlet-class bingo!

4. Reference material
csdn: http://blog.csdn.net/shanliangliuxing/article/details/7458492

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325485753&siteId=291194637