The role of web.xml in the web project, how to configure the project without web.xml

There can be no web.xml file in a web, that is to say, the web.xml file is not necessary for a web project.  When do you need it and when do you need it? To answer the above question, you must first understand what the web.xml file is used for. The web.xml file is used to configure: welcome page, servlet, filter, etc. When your web project does not use these, you can configure your web project without the web.xml file. So what are all the things web.xml can do? In fact, 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 those functions defined. The schema file of web.xml is defined by Sun. The root element <web-app> of each web.xml file must indicate which schema file the web.xml uses. For example:  <?xml version="1.0" encoding="UTF-8"?>  <web-app version="2.5"  xmlns="http://java.sun.com/xml/ns/javaee"  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>   

 
 
 
 
 
 
 
 
 
 
 
Moreover, the tags defined in the schema file of web.xml are not fixed, and the schema file can also be changed. Generally speaking, with the version upgrade of the web.mxl schema file, the functions defined in it will become more and more complicated, and the That is, there will be more and more types of label elements, but some are not commonly used, we only need to remember some commonly used ones.  The following is a list of commonly used tag elements in web.xml and their functions:  1. Specify the welcome page, for example:  <welcome-file-list>  <welcome-file-list>  <welcome-file>index.jsp</welcome -file>  <welcome-file>index1.jsp</welcome-file>  </welcome-file-list>  The above example specifies two welcome pages, and they are displayed in order from the first one, if the first one If it exists, the first one will be displayed, and the following ones will not work. If the first one doesn't exist, find the second one, and so on. About the welcome page:  When visiting a website, the first page you see by default is called the welcome page. Generally, the home page serves as the welcome page. Under normal circumstances, we will specify the welcome page in web.xml. But web.xml is not a necessary file for the Web. Without web.xml, the website can still work normally. It's just that after the function of the website is complicated, web.xml is indeed very useful. Therefore, the dynamic web project created by default has a web.xml file under the WEB-INF folder.   

 
 
 
 
 
 
 
 
 
 
For tomcat, when you only specify the root name of a web and do not specify a specific page, when you visit a web, if the welcome page is configured in the web.xml file, then the specified page is returned as the welcome page, and If there is no web.xml file in the text, or although there is web.xml, but web.xml does not specify the welcome page, it will look for the index.html file by default. If it is found, it will use the index.html as the welcome page. back to the browser. If index.html is not found, tomcat goes to index.jsp. Find index.jsp and return it as the welcome page. If neither index.html nor index.jsp is found, and the welcome page is not specified with the web.xml file, then tomcat does not know which file to return, and it displays The requested resource (/XXX) is not available page. Where XXX represents the root name of the web. But if you specify a specific page, it can be accessed normally.  2. Naming and custom URL. We can name and customize the URL for Servlet and JSP files, where the custom URL is dependent on a name, and the name must be before the custom URL. Let's take serlet as an example:  (1) Name the servlet:  <servlet>  <servlet-name>servlet1</servlet-name>  <servlet-class>net.test.TestServlet</servlet-class>  </servlet>  ( 2), customize the URL for the servlet,  <servlet-mapping>  <servlet-name>servlet1</servlet-name>   
 
 
 
 
 
 
 
 
 
 
</servlet-mapping>  3. Customize initialization parameters: You can customize the initialization parameters of servlet, JSP, and Context, and then you can get these parameter values ​​from servlet, JSP, and Context. Which servlet is used as an example:  <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>Tommy@163. com</param-value>  </init-param>  </servlet>  After the above configuration, getServletConfig().getInitParameter("param1") can be called in the servlet to obtain the value corresponding to the parameter name. 4. Specify the error handling page. You can specify the error handling page by "Exception Type" or "Error Code". <error-page>   

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
</error-page>   
-----------------------------   
<error-page>   
<exception-type>java.lang.Exception<exception-type>   
<location>/exception.jsp<location>   
</error-page>   
5、设置过滤器:比如设置一个编码过滤器,过滤所有资源   
<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、设置监听器:   
<listener>   
<listener-class>net.test.XXXLisenet</listener-class>   
</listener>   
7、设置会话(Session)过期时间,其中时间以分钟为单位,假如设置60分钟超时:   
<session-config>   
<session-timeout>60</session-timeout>   
</session-config>   
除了这些标签元素之外,还可以往web.xml中添加那些标签元素呢,那些标签元素都能起什么作用呢?我们只要去查看web.xml的模式文件就能知道。直接看模式文件看不懂,可以找一些中文教程来看看。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326661399&siteId=291194637