使用过滤器之后页面显示乱码怎么回事?
我写了一个过滤器 判断用户是够登录的
没加过滤器之前页面还能正常显示 但是加上之后页面上的中文就变乱码了
过滤器这样写的
package myFilter;
import
java.io.IOException;
import java.io.PrintWriter;
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;
import javax.servlet.http.HttpSession;
public class ContentTypeFilter implements Filter {
private FilterConfig filterConfig;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws
IOException,
ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpSession session=req.getSession();
PrintWriter out=response.getWriter();
if(session.getAttribute( "admin ")==null){
out.println( " <html> ");
out.println( " <head> ");
out.println( " </head> ");
out.println( " <script> ");
out.println( "window.top.location.href= 'index.jsp '; ");
out.println( " </script> ");
out.println( " </html> ");
return;
}else{
/*刚开始这里没加
request.setCharacterEncoding( "gbk ");
response.setContentType( "text/html;charset=gbk ");
response.setCharacterEncoding( "gbk ");
显示的是乱码 加上之后还是乱码*/
filterChain.doFilter(request, response);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
}
}
怎么解决啊 项目等这做呢 现在每个页面都是乱码 第一次遇到 大家帮帮忙解决下
分不多 所以还请多多帮忙
------解决方案--------------------tomcat的配置文件server.xml里这句:
<Connector URIEncoding= "UTF-8 "
port= "8080 " maxHttpHeaderSize= "8192 "
maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
connectionTimeout= "20000 " disableUploadTimeout= "true " />
加上这句就行了:URIEncoding= "UTF-8 "
UTF-8或者你自己的什么编码,比如GBK等
------解决方案--------------------request.setCharacterEncoding( "gb2312 ");
看看行不行
------解决方案--------------------web.xml里是否放置了多个过滤器,考虑调一下顺序
------解决方案--------------------你参考参考我写的:
filter类:
package org.battwofame.hellojava.telservlet.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import ja