下面问题中jsp中文乱码求解决-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

下面问题中jsp中文乱码求解决

落地花开啦 2016-06-02 14:28:29 1468

敏感字过滤:不能替换中文;代码如下。
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>
Java
分享到
取消 提交回答
全部回答(1)
  • 落地花开啦
    2019-07-17 19:24:50

    form默认的提交方式是get,get方式会导致中文乱
    改成post 试下
    `


    `
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程