在java工程中,web.xml用来初始化工程配置信息,比如说welcome页面,filter,listener,servlet,servlet-mapping,启动加载级别等等。
根元素标签<web-app>
每个web.xml文件的根元素为<web-app>中,必须标明这个web.xml使用的是哪个模式文件。
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
............
.........
..........
</web-app>
<display-name>标签
<display-name>springDCIM</display-name>
<welcome-file-list>标签
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
这样,访问网址http://localhost:8080/test时可以跳转到index.jsp页面
如果我创建一个Action,在struts.xml中配置一个访问wel.do访问路径,跳转到Action类中的excute方法中
,执行这里面的return mapping.findForward("index");然后修改<welcome-file>节点内容为wel.do则再次访问的时候就会报404错误
监听器有HttpSessionListener HttpSessionAttributeList
ener ServletContextListener <listener>
<listener-class>org.xiosu.listener.onlineListener</listener-class>
</listener>
public class onlineListener implements HttpSessionListener,
HttpSessionAttributeListener {
// 参数
ServletContext sc;
ArrayList list = new ArrayList();
// 新建一个session时触发此操作
public void sessionCreated(HttpSessionEvent se) {
sc=se.getSession().getServletContext();
System.out.println("新建一个session");
}
// 销毁一个session时触发此操作
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("销毁一个session");
if (!list.isEmpty()) {
list.remove((String) se.getSession().getAttribute("userName"));
sc.setAttribute("list", list);
}
}
// 在session中添加对象时触发此操作,在list中添加一个对象
public void attributeAdded(HttpSessionBindingEvent sbe) {
list.add((String) sbe.getValue());
sc.setAttribute("list", list);
}
// 修改、删除session中添加对象时触发此操作
public void attributeRemoved(HttpSessionBindingEvent arg0) {
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
}
}
常用的监听接口有以下几个:
<filter>标签
ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。
ServletContextListener监听ServletContext。当创建ServletContext时,激发 contextInitialized(ServletContextEventsce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发sessionCreated(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发 attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
<servlet>标签
<context-param>标签上下文参数
如果是在<servlet>标签中配置init-param参数,只能由这个servlet来读取,
如果配置一个所有servlet都能读取的参数,就要在外面配置<context-param>
除了这种在servlet中读取资源的方法,还有就是通过@Resource标注来字符串变量message,在web.xml中配置一个名为messageNanmeInWebXml的参数就可以了,
<error>标签
web.xml中设计websocket的配置
上面的WebSocket在web.xml中配置了,websocket文件中注解才生效
web.xml中设计spring框架的配置
ContextLoaderListener是Spring的监听器,它的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
这段配置是用于指定applicationContext.xml配置文件的位置,可通过context-param加以指定:
这里需要搞清楚classpath是什么,以及classpath:和classpath*有何区别:
1. 首先 classpath是指 WEB-INF文件夹下的classes目录
2. classpath 和 classpath* 区别:
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.
如果applicationContext.xml配置文件存放在src目录下,就好比上面的代码结构中的存放位置,那么在web.xml中的配置就如下所示:
如果applicationContext.xml配置文件存放在WEB-INF下面,那么在web.xml中的配置就如下所示:
需要注意的是,部署到应用服务器后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下,spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml, 运行时使用的是web-info/classes目录下的applicationContext.xml。因此,不管applicationContext.xml配置文件存放在src目录下,还是存放在WEB-INF下面,都可以用下面这种方式来配置路径:
当有多个配置文件加载时,可采用下面代码来配置:
复制代码
复制代码
也可以用下面的这种方式:
"**/"表示的是任意目录;
"**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。
Spring配置文件最好以"applicationContext-"开头,且最好把所有Spring配置文件都放在一个统一的目录下,也可以分模块创建。