Java web项目中web.xml的配置
2016-08-31 13:33
375 查看
(转载)http://blog.csdn.net/dreamrealised/article/details/9123201
在java的web项目中,我们都能发现一个web.xml文档,但是这个文档并不是必须的,web.xml的主要功能是用来初始化项目工程的配置信息,比如说welcome页面,filter,servlet等,当你的web工程中没用到这些当然也就不需要这个xml文件来配置你的apllication。
下面简单介绍web.xml的几个功能的配置和作用:
1.context-param
容器启动后创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.容器将转化为键值对,并交给ServletContext。
[html] view
plain copy
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.xkxt.root</param-value>
</context-param>
上面这个context-param的配置主要是区分tomcat的webapp下的不同项目,能够让log能将日志写到对应项目根目录下。
[html] view
plain copy
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
上面的配置信息是建立log4j的键值对并且建立log4j的监听。
[html] view
plain copy
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
上面的配置是启动web的时候初始化spring的配置文档applicationContext.xml。applicationContext.xml的配置详见http://blog.sina.com.cn/s/blog_6cad92b701019thl.html。
2.配置拦截器dispatcher
[html] view
plain copy
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.aj</url-pattern>
</servlet-mapping>
以上配置是针对拦截器,可以看到拦截器文档的名字应该叫做dispatcher-servlet.xml,拦截的url请求是以do和aj为结尾。具体的dispatcher-servlet.xml的配置请参见:后续会详细介绍dispatcher-servlet.xml。
3.welcome-file-list
[html] view
plain copy
<welcome-file-list>
<welcome-file>main.do</welcome-file>
</welcome-file-list>
该标签是为了指定欢迎列表,可以配置多个welcome-file,显示时候按照顺序从第一个开始找起。对于tomcat来说,当你只指定一个web的根名,没有指定具体页面,去访问时一个web时,如果web.xml文件中配置了欢迎页,那么就返回指定的那个页面作为欢迎页,如果web.xml中没指定欢迎页的情况下,它默认先查找index.html文件,如果找到了,就把index.html作为欢迎页还回给浏览器。如果没找到index.html,tomcat就去找index.jsp。找到index.jsp就把它作为欢迎页面返回。而如果index.html和index.jsp都没找到,那此时tomcat就不知道该返回哪个文件,那么前端会报错。
4.过滤器
[html] view
plain copy
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>nju.software.xkxt.web.filter.SetEncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
该过滤器主要是解决前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候出现的表单提交的数据或者上传/下载中文名称文件出现乱码的问题。其中filter标签内部配置的是过滤器的名字和具体的类。而filter-mapping中配置的是过滤器对什么文件有效,此处在页面请求.do操作的时候会进行拦截,然后执行过滤器代码。其中SetEncodingFilter类的具体代码如下:
[java] view
plain copy
package nju.software.xkxt.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* @author typ
*
*/
public class SetEncodeFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void destroy() {
// TODO Auto-generated method stub
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
该类需要继承Filter类,并实现Filter类中的三个抽象方法:doFilter,destroy,init。doFilter中的request.setCharacterEncoding("utf-8");语句的作用是设置用户界面提交的数据是以utf-8编码格式提交到服务器。
encoding用来指定一个具体的字符集 forceEncoding作用很单一,当request中已经被指定了一个字符集的时候是否再将用endcoding对应的字符集设置到request中去。举个例子来说明,假如forceEncoding=false,当请求被提交之后,过滤器会判断request.getCharacterEncoding()是否为null,如果是null那么就会进行request.setCharacterEncoding("UTF-8")的操作,如果不是null那么过滤器什么也不会做。
在java的web项目中,我们都能发现一个web.xml文档,但是这个文档并不是必须的,web.xml的主要功能是用来初始化项目工程的配置信息,比如说welcome页面,filter,servlet等,当你的web工程中没用到这些当然也就不需要这个xml文件来配置你的apllication。
下面简单介绍web.xml的几个功能的配置和作用:
1.context-param
容器启动后创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.容器将转化为键值对,并交给ServletContext。
[html] view
plain copy
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.xkxt.root</param-value>
</context-param>
上面这个context-param的配置主要是区分tomcat的webapp下的不同项目,能够让log能将日志写到对应项目根目录下。
[html] view
plain copy
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
上面的配置信息是建立log4j的键值对并且建立log4j的监听。
[html] view
plain copy
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
上面的配置是启动web的时候初始化spring的配置文档applicationContext.xml。applicationContext.xml的配置详见http://blog.sina.com.cn/s/blog_6cad92b701019thl.html。
2.配置拦截器dispatcher
[html] view
plain copy
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.aj</url-pattern>
</servlet-mapping>
以上配置是针对拦截器,可以看到拦截器文档的名字应该叫做dispatcher-servlet.xml,拦截的url请求是以do和aj为结尾。具体的dispatcher-servlet.xml的配置请参见:后续会详细介绍dispatcher-servlet.xml。
3.welcome-file-list
[html] view
plain copy
<welcome-file-list>
<welcome-file>main.do</welcome-file>
</welcome-file-list>
该标签是为了指定欢迎列表,可以配置多个welcome-file,显示时候按照顺序从第一个开始找起。对于tomcat来说,当你只指定一个web的根名,没有指定具体页面,去访问时一个web时,如果web.xml文件中配置了欢迎页,那么就返回指定的那个页面作为欢迎页,如果web.xml中没指定欢迎页的情况下,它默认先查找index.html文件,如果找到了,就把index.html作为欢迎页还回给浏览器。如果没找到index.html,tomcat就去找index.jsp。找到index.jsp就把它作为欢迎页面返回。而如果index.html和index.jsp都没找到,那此时tomcat就不知道该返回哪个文件,那么前端会报错。
4.过滤器
[html] view
plain copy
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>nju.software.xkxt.web.filter.SetEncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
该过滤器主要是解决前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候出现的表单提交的数据或者上传/下载中文名称文件出现乱码的问题。其中filter标签内部配置的是过滤器的名字和具体的类。而filter-mapping中配置的是过滤器对什么文件有效,此处在页面请求.do操作的时候会进行拦截,然后执行过滤器代码。其中SetEncodingFilter类的具体代码如下:
[java] view
plain copy
package nju.software.xkxt.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* @author typ
*
*/
public class SetEncodeFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void destroy() {
// TODO Auto-generated method stub
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
该类需要继承Filter类,并实现Filter类中的三个抽象方法:doFilter,destroy,init。doFilter中的request.setCharacterEncoding("utf-8");语句的作用是设置用户界面提交的数据是以utf-8编码格式提交到服务器。
encoding用来指定一个具体的字符集 forceEncoding作用很单一,当request中已经被指定了一个字符集的时候是否再将用endcoding对应的字符集设置到request中去。举个例子来说明,假如forceEncoding=false,当请求被提交之后,过滤器会判断request.getCharacterEncoding()是否为null,如果是null那么就会进行request.setCharacterEncoding("UTF-8")的操作,如果不是null那么过滤器什么也不会做。
相关文章推荐
- Java web 项目 web.xml 配置文件加载过程
- java web 项目中的web.xml配置详解
- Java Web项目web.xml配置详解与示例
- java Web项目javaEE中web.xml文件中配置 contextConfigLocation 方法
- Java web项目中web.xml的配置
- Struts 项目中web.xml文件配置
- web项目web.xml的配置中配置是起什么作用
- Java:Struts 项目中web.xml文件配置
- eclipse web项目配置server.xml(maven)
- java web开发中 web.xml文件的作用及常用配置说明
- web项目的web.xml配置详细说明
- Eclipse JEE 3.6 以上版本新建web项目后,WEB-INF下没有web.xml配置文件
- Struts 项目中web.xml文件配置
- Web项目中web.xml配置文件context-param作用
- 关于 Eclipse 创建 Dynamic Web Project (动态的 WEB 项目) 为什么会出现没有web.xml配置文件的问题
- j2ee项目开发web.xml文件加载及配置详解
- web项目web.xml的配置中<context-param>配置是起什么作用的
- Java Web_web.xml配置404和500错误及异常
- 基于S2SH框架的项目—web.xml文件配置
- web项目web.xml的配置中配置是起什么作用