开发者社区> 问答> 正文

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

敏感字过滤:不能替换中文;代码如下。
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>

展开
收起
落地花开啦 2016-06-02 14:28:29 2929 0
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

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


    `
    2019-07-17 19:24:50
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载