当前位置:编程学习 > JAVA >>

关于PrintWriter导致乱码????

public class SecurityFilter implements Filter {

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest=(HttpServletRequest)request;
HttpServletResponse httpResponse=(HttpServletResponse)response;
HttpSession session=httpRequest.getSession();
//PrintWriter out=response.getWriter(); 会导致输出的网页乱码,为什么?
if(session.getAttribute("admin")==null){
PrintWriter out=response.getWriter();
out.print("<script>alert('请登录...');location.href='"+httpRequest.getContextPath()+"/front/adminlogin.html'</script>");
}else{
chain.doFilter(request, response);
}
}

public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}

} --------------------编程问答-------------------- 那你就写一个过滤器啦,就ok了啊,在编程过程中,难免避免乱码 --------------------编程问答-------------------- 能有更明确的回答吗? --------------------编程问答-------------------- response.setCharacterEncoding("UTF-8"); 加下这个代码看还会有不?写在response.getWriter(); 前面。 --------------------编程问答-------------------- 你的乱码应该是“请登录。。。”这几个汉字报的错误
你把它转换一下
new String("请登录".getBytes("UTF-8")); --------------------编程问答--------------------
引用 3 楼  的回复:
response.setCharacterEncoding("UTF-8"); 加下这个代码看还会有不?写在response.getWriter(); 前面。


顶!应该就是这个问题! --------------------编程问答--------------------  你是不是没有中文过滤器。。 --------------------编程问答-------------------- 有过滤器,而且设置的是过滤整个根目录。
就 我说的网页乱码  是会导致浏览器访问的路径错误?
根本不会访问到想要的servlet

强调一下把  //PrintWriter out=response.getWriter();  
写在if里面就什么问题都没有。 --------------------编程问答--------------------
引用 7 楼  的回复:
有过滤器,而且设置的是过滤整个根目录。
就 我说的网页乱码 是会导致浏览器访问的路径错误?
根本不会访问到想要的servlet

强调一下把 //PrintWriter out=response.getWriter();  
写在if里面就什么问题都没有。


怎么可能啊。。。这有点让人不敢相信噢。。。 --------------------编程问答-------------------- 这个问题也郁闷了我很久,最后是这样解决的。注意代码的顺序。
httpResponse.setContentType("text/html");
httpResponse.setHeader("Cache-Control","no-cache");
httpResponse.setCharacterEncoding("GB2312");

PrintWriter out = httpResponse.getWriter();
out.write("<script>alert('用户登录过期!');window.top.location.href='login.ejf?cmd=logout';</script>");
out.flush();

return; --------------------编程问答-------------------- 不是printwriter的问题,加一个过滤器,就像三楼那样,就可以解决了  --------------------编程问答--------------------
引用 3 楼 m525610032 的回复:
response.setCharacterEncoding("UTF-8"); 加下这个代码看还会有不?写在response.getWriter(); 前面。

+1

我才学java在Servlet中也用到过这个,先设置你的编码格式输出才不会有问题,还有你的字符串最好也要改下相应的格式比如String Name Request.getParameter("Name");
Name=new String(Name.getBytes("iso-8859-1")); --------------------编程问答--------------------
引用 7 楼 wangqingninja 的回复:
有过滤器,而且设置的是过滤整个根目录。
就 我说的网页乱码  是会导致浏览器访问的路径错误?
根本不会访问到想要的servlet

强调一下把  //PrintWriter out=response.getWriter();  
写在if里面就什么问题都没有。
不懂 观望结果 --------------------编程问答--------------------
引用 10 楼 yangyang0209 的回复:
不是printwriter的问题,加一个过滤器,就像三楼那样,就可以解决了



加过滤器好加,问题是过滤器里怎么写代码,来处理乱码问题呢?

代码实例贴一下呗。 --------------------编程问答-------------------- response.setContentType("text/html;charset=utf-8"); --------------------编程问答-------------------- 你写一个过滤器就行了啊 ,或者简单点就直接加上
response.setContentType("text/html;charset=utf-8");就OK了啊  --------------------编程问答-------------------- 你也可以直接到根目录下有个配置文件 修改下 Encoding改成"utf-8"
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,