学会使用filter过滤器过滤字符集,以及如何配置web.xml
2014-09-12 20:26
519 查看
1.tomcat启动后会加载web.xml,初始化一个实现filter接口的对象。
2.每次客户端访问服务器端的时候(符合<url-patternd>的范围)都会调用dofliter方法。
3.不使用chain.dofliter(),程序不会继续执行。
4.支持post方法,不支持get方法
[b]com.hlx.filter.Charset_filter.java[/b]
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;
public class Charset_filter implements Filter {
private String encoding;
public Charset_filter() {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//方法一:设置字符集
//request.setCharacterEncoding("UTF-8");
//方法二:使用变量设置字符集,变量由web.xml初始化,作为配置工具使用
request.setCharacterEncoding(encoding);
//继续执行
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
//方法二:读取web.xml里的内容
encoding = config.getInitParameter("encoding");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>filterConfig</filter-name>
<!-- 一定不是com.hlx.filter.Charset_filter.java,不要加.java,否则返回404错误,根本打不开页面 -->
<filter-class>com.hlx.filter.Charset_filter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filterConfig</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>
2.每次客户端访问服务器端的时候(符合<url-patternd>的范围)都会调用dofliter方法。
3.不使用chain.dofliter(),程序不会继续执行。
4.支持post方法,不支持get方法
[b]com.hlx.filter.Charset_filter.java[/b]
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;
public class Charset_filter implements Filter {
private String encoding;
public Charset_filter() {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//方法一:设置字符集
//request.setCharacterEncoding("UTF-8");
//方法二:使用变量设置字符集,变量由web.xml初始化,作为配置工具使用
request.setCharacterEncoding(encoding);
//继续执行
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
//方法二:读取web.xml里的内容
encoding = config.getInitParameter("encoding");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>filterConfig</filter-name>
<!-- 一定不是com.hlx.filter.Charset_filter.java,不要加.java,否则返回404错误,根本打不开页面 -->
<filter-class>com.hlx.filter.Charset_filter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filterConfig</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>
相关文章推荐
- 如何使用web.xml配置参数,并在serlvet取得该值
- spring 在web.xml 里面如何使用多个xml配置文件
- 如何在web.xml配置spring,struts2配置以及过滤器
- 如何使用Javaconfig代替web.xml配置spring
- Spring-property文件配置方法以及如何与工程分离~xml使用${}
- Struts2.3使用Sitemesh如何配置web.xml?
- spring 在web.xml 里面如何使用多个xml配置文件
- 修改web.xml 去掉jetty锁定静态文件的问题 的配置(正点-使用maven,jetty配合)
- Jsp中errorPage使用及web.xml配置
- 转载:如何使用XCodeFactory自动生成XML配置文件和对应的解析类
- 如何做用XML配置的web菜单程序
- 如何配置订阅以使用 Web 同步(RMO 编程)【转载】
- tomcat和apache整合之后如何配置数据源,以及web应用程序应该放到哪里呢
- spring如何使用多个xml配置文件 【转】
- 项目中使用Spring时配置web.xml的listener侦听接口不能初始化的问题
- 一是最简单的例子让你学会如何使用XML来做一个页面
- Web设计中如何使用XML数据源对象
- 如何通过配置tomcat或是web.xml让ie直接下载txt类型的文件
- 用javascript操作xml-->Web设计中如何使用XML数据源对象(转载)
- Web设计中如何使用XML数据源对象(2)