编写jsp的web.xml文件的方法:首先我们需要在文件中指定欢迎页;然后需要命名并定制URL;接着需要定制初始化参数、指定错误处理页面;最后设置过滤器和监听器即可。

先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对。
web.xml是不是必须的呢?不是的,只要你不用到里面的配置信息就好了,不过在大型web工程下使用该文件是很方便的,若是没有也会很复杂。
推荐课程:java课程
那么web.xml能做的所有事情都有那些?其实,web.xml的模式(Schema)文件中定义了多少种标签元素,web.xml中就可以出现它的模式文件所定义的标签元素,它就能拥有定义出来的那些功能。web.xml的模式文件是由Sun公司定义的,每个web.xml文件的根元素中,都必须标明这个web.xml使用的是哪个模式文件。
来看个例子:
1 2 3 4 5 6 7 8 9 10 11 12 | <?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>
|
Copy after login
二.标签元素
指定欢迎页面
1 2 3 4 | <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index1.jsp</welcome-file>
</welcome-file-list>
|
Copy after login
上面的例子指定了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
1 2 3 4 5 6 7 8 9 | <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>
|
Copy after login
url-pattern的意思是所有的.do文件都会经过TestServlet处理。
定制初始化参数
1 2 3 4 5 6 7 8 9 10 11 12 | <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>
|
Copy after login
经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("param1")获得参数名对应的值。
1 2 3 4 5 6 7 | <context-param>
<param-name>ContextParameter</para-name>
<param-value>test</param-value>
<description>It is a test parameter.</description>
</context-param>
|
Copy after login
指定错误处理页面,可以通过“异常类型”或“错误码”来指定错误处理页面。
1 2 3 4 5 6 7 8 9 10 11 12 13 | <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>
|
Copy after login
设置过滤器:比如设置一个编码过滤器,过滤所有资源
1 2 3 4 5 6 7 8 | <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>
|
Copy after login
设置监听器
web.xml中的有什么用? 没别的用处!就是配置监听类的~,它能捕捉到服务器的启动和停止! 在启动和停止触发里面的方法做相应的操作! 它必须在web.xml 中配置才能使用! web.xml 中listener元素不是只能有一个,有多个时按顺序执行。
如何在web.xml向listener中传参数 ?
1 2 3 | <listener>
<listener- class >监听器类的完整路径</listener- class >
</listener>
|
Copy after login
监听器中不能够写初始化参数; 可通过另个的途径达到初始化参数的效果:
1.写一个properties文件,在文件里写好初始化参数值, 2.在监听器中可以通得到properties文件中的值(写在静态块中)。
设置会话(Session)过期时间,其中时间以分钟为单位
1 2 3 | <session-config>
<session-timeout>60</session-timeout>
</session-config>
|
Copy after login
除了这些标签元素之外,还可以往web.xml中添加那些标签元素呢,那些标签元素都能起什么作用呢?
我们只要去查看web.xml的模式文件就能知道。直接看模式文件看不懂,可以找一些中文教程来看看。
相关推荐:java入门教程
The above is the detailed content of How to write the web.xml file of jsp. For more information, please follow other related articles on the PHP Chinese website!