jsp中文乱码求解决

liuhuics10 发布于 2015/09/25 16:11
阅读 290
收藏 0

敏感字过滤:不能替换中文;代码如下。

myeclipse 和project的text file encoding皆utf-8. 


1.filter/comments.jsp:
<%@ page language ="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://" +request.getServerName()+":"+request.getServerPort()+path+ "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 
  <body >
     <form   action="commentsResult.jsp">
          用户名: <input type ="text" name="uname" /><br/>
          评论: <textarea rows ="7" cols="10" name= "comments"></textarea ><br/>
           <input type ="submit" value="提交"/>
     </form >
  </body >
</html>
2.filter/commentsFilter.java:
package 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;

public class CommentsFilter implements Filter{
     public void destroy() {
           // TODO Auto-generated method stub
          
     }
     public void doFilter(ServletRequest request, ServletResponse response,
              FilterChain chain) throws IOException, ServletException {
          request.setCharacterEncoding( "utf-8"); //
          response.setContentType( "text/html; charset=UTF-8");
          String uname=request.getParameter( "uname");
          String comments=request.getParameter( "comments");
           if( null!=uname){
              uname=uname.replace( "hui", "#");
               //若是替换中文,则页面显示乱码,尚未解决
              request.setAttribute( "uname", uname);
          }
           if( null!= comments){
               comments=comments .replace("dang" , "*" );
              request.setAttribute( "comments", comments);
          }
          chain.doFilter(request, response);
     }
     public void init(FilterConfig filterConfig) throws ServletException {
     }
}
同时在web.xml中配置如下:
 <filter >
           <filter-name >CommentsFilter </filter-name>
           <filter-class >filter.CommentsFilter </filter-class>
     </filter >
     
     <filter-mapping >
           <filter-name >CommentsFilter </filter-name>
           <url-pattern >/filter/commentsResult.jsp </url-pattern>
     </filter-mapping > 
3.filter/commentsResult.jsp:
<%@ page language ="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://" +request.getServerName()+":"+request.getServerPort()+path+ "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body >
     用户名: <%=request.getAttribute( "uname") %><br />
     评论: <%=request.getAttribute( "comments") %>
  </body >
</html>


加载中
0
vidy_tu
vidy_tu

form默认的提交方式是get,get方式会导致中文乱

改成post 试下

<form name=”form” method=”post”>

</form>





liuhuics10
liuhuics10
果然是对的了,你太厉害了
0
xpbob
xpbob
显示页面你也做中文处理吧,可能从域中取出的东西,传输也是iso-8859-1
0
r
runrunsnail

以你的写法,数据传送时,非UTF-8,html里下面的设定尝试一下。

  <meta charset="utf-8" /> 
  <meta http-equiv="content-language" content="zh-CN" />

liuhuics10
liuhuics10
试了,也不行
0
大海
大海
浏览器编码看一下是不是utf8
liuhuics10
liuhuics10
用的firefox浏览器,编码是unicode
0
不可思忆
真相只有一个:tomcat(或者其他容器)也要配置编码。不然传输时还是按iso8859-1来的;或者直接暴力解决:new String("参数".getBytes("iso8859-1"), "utf-8")
liuhuics10
liuhuics10
tomcat的server.xml中已经配置:<?xml version='1.0' encoding='utf-8'?>
返回顶部
顶部