懂java的朋友帮我看下这个过滤器写的对不对 10
1个回答
展开全部
就这点信息还不够!配置没有问题,检查一下是不是实现接口的类写错了。
给楼主一个关于字符乱码的过滤器例子,希望对楼主有帮助!
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>CharsetFilter</filter-name>
<filter-class>com.hb.filter.CharsetFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
实现接口的类:
package com.hb.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;
import javax.servlet.http.HttpServletRequest;
public class CharsetFilter implements Filter {
protected FilterConfig filterConfig;
private String targetEncoding = "UTF-8";
/**
* 初始化过滤器,和一般的Servlet一样,它也可以获得初始参数。
*/
public void init(FilterConfig config) throws ServletException {
this.filterConfig = config;
this.targetEncoding = config.getInitParameter("encoding");
}
/**
* 进行过滤处理,这个方法最重要,所有过滤处理的代码都在此实现。
*/
public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {
System.out.println("使用以下方法对请求进行编码:encoding=" + targetEncoding);
HttpServletRequest request = (HttpServletRequest) srequest;
request.setCharacterEncoding(targetEncoding);
// 把处理权发送到下一个
chain.doFilter(srequest, sresponse);
}
public void setFilterConfig(final FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
// 销毁过滤器
public void destroy() {
this.filterConfig = null;
}
}
给楼主一个关于字符乱码的过滤器例子,希望对楼主有帮助!
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>CharsetFilter</filter-name>
<filter-class>com.hb.filter.CharsetFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
实现接口的类:
package com.hb.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;
import javax.servlet.http.HttpServletRequest;
public class CharsetFilter implements Filter {
protected FilterConfig filterConfig;
private String targetEncoding = "UTF-8";
/**
* 初始化过滤器,和一般的Servlet一样,它也可以获得初始参数。
*/
public void init(FilterConfig config) throws ServletException {
this.filterConfig = config;
this.targetEncoding = config.getInitParameter("encoding");
}
/**
* 进行过滤处理,这个方法最重要,所有过滤处理的代码都在此实现。
*/
public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {
System.out.println("使用以下方法对请求进行编码:encoding=" + targetEncoding);
HttpServletRequest request = (HttpServletRequest) srequest;
request.setCharacterEncoding(targetEncoding);
// 把处理权发送到下一个
chain.doFilter(srequest, sresponse);
}
public void setFilterConfig(final FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
// 销毁过滤器
public void destroy() {
this.filterConfig = null;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询