Servlet 中 web.xml 文件的配置问题
2012-06-08 16:44
381 查看
博客分类:
文件的位置
web.xml文件位于WEB应用下的WEB-INF
基本格式
此xml文件需要有一个根:web-app
如何为servlet命名
可以通过servlet-name和servlet-class元素指定servlet的名字和实际的类路径,如
指定了WEB应用下的classes目录下的servletDemo.helloServlet类的名字,相当于一个别名,用于简化。这个名字可以在下面的URL定制中使用。需要注意的是servlet-class的属性需要用“.”号分隔开。
为已命名的servlet制定URL
这样指定URL后,就可以在浏览器的地址栏中使用以下形式访问servletDemo.helloServlet类了:
http://localhost:8080/web_application_name/helloServlet(主机名和端口跟你自己设定的有关,web_application_name即为你的WEB应用的名字)。这里需要注意的是,url-pattern的属性需要用“/”号分隔开,与servlet-class不同。
当然,可以在一个web.xml中声明很多个servlet,名字不能相同,下面给出一个例子:
![](http://byfiles.storage.msn.com/y1pViB8kuCrcaGPYVonGfWpjVznvYIZyeGMUv2lMWDHNvX2zKGUQIY_7Xr-2OeB0Mek?PARTNER=WRITER)
在浏览器中键入:http://localhost:8080/ServletTest/HelloServlet即可访问ServletTest应用下的classes目录下的servletDemo.HelloServlet类了。
![](http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-4443501334797508957&page=RSS%3a+Servlet+%e4%b8%ad+web.xml+%e6%96%87%e4%bb%b6%e7%9a%84%e9%85%8d%e7%bd%ae%e9%97%ae%e9%a2%98&referrer=)
web.xml文件之配置
在Servlet的测试时,自己花费了很大的时间,现在把我自己的经验分享一下,免得大家走弯路。文件的位置
web.xml文件位于WEB应用下的WEB-INF
基本格式
此xml文件需要有一个根:web-app
如何为servlet命名
可以通过servlet-name和servlet-class元素指定servlet的名字和实际的类路径,如
servlet-name | helloServlet |
servlet-class | servletDemo.helloServlet |
为已命名的servlet制定URL
servlet-name | helloServlet |
url-pattern | /helloServlet |
http://localhost:8080/web_application_name/helloServlet(主机名和端口跟你自己设定的有关,web_application_name即为你的WEB应用的名字)。这里需要注意的是,url-pattern的属性需要用“/”号分隔开,与servlet-class不同。
当然,可以在一个web.xml中声明很多个servlet,名字不能相同,下面给出一个例子:
在浏览器中键入:http://localhost:8080/ServletTest/HelloServlet即可访问ServletTest应用下的classes目录下的servletDemo.HelloServlet类了。
![](http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-4443501334797508957&page=RSS%3a+Servlet+%e4%b8%ad+web.xml+%e6%96%87%e4%bb%b6%e7%9a%84%e9%85%8d%e7%bd%ae%e9%97%ae%e9%a2%98&referrer=)
![](http://c.live.com/c.gif?NC=31263&NA=1149&PI=73329&RF=&DI=3919&PS=85545&TP=kmustlinux.spaces.live.com>1=kmustlinux)
相关文章推荐
- Servlet 中 web.xml 文件的配置问题
- Spring配置web.xml文件时遇到的问题
- web.xml配置文件详解之一servlet配置
- 谷歌验证码非web.xml非servlet拦截 使用配置文件自定义加载,防止多次点击(防js攻击)
- Servlet 配置文件web.xml
- servlet中配置文件web.xml中的参数context-param和init-param
- spring mvc解决中文乱码问题需在web.xml文件中添加下面的配置
- spring mvc框架 web.xml配置文件 Could not open ServletContext resource [/WEB-INF/classes/spring-servlet.xml] 错误
- springMVC配置文件web.xml与spring-servlet.xml与spring-jdbc.xml与logback.xml与redis.properties与pom.xml
- [错误]web.xml文件配置错误 servlet-name配置不一致
- Servlet各版本web.xml头文件配置模板
- eclipse项目web.xml文件配置servlet映射
- SpringMVC(1):基础配置文件web.xml 和 <servlet-name>-servlet.xml / springmvc.xml
- web.xml文件配置中出现的各种问题 4000
- 关于web项目web.xml配置文件路径的问题。
- javaweb_servlet_web.xml文件配置
- 在web.xml 文件中servlet的配置
- SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】
- Servlet各版本web.xml的头文件配置模板
- Servlet(19)web.xml 配置之Servlet参数配置和读取资源文件,获取文件全路径