• 您所在的位置:点点通>> 主页>网络学院>网站编程>ASP教程>解决JSP中使用request乱码问题-JSP教程-网络编程-iT粉丝网 专注网页设计制作教程:HTTP://WWW.WEBJX.COM

解决JSP中使用request乱码问题-JSP教程-网络编程-iT粉丝网 专注网页设计制作教程:HTTP://WWW.WEBJX.COM

时间:2008-11-09 来源: 网友评论0条
    JSP显示中文有乱码怎么办,用request得到的用户输入的中文怎么是乱码,把汉字写到数据库怎么是乱码,等等一些关于汉字乱码的问题。其实这个问题很简单,管它汉字不汉字,还是日文,还是其他的什么双字节的语言,我们一律把它当作UTF-8看待。

      (一)request中的双字节文字

      我们来实现在整个应用程序中使用UTF-8编码工作,之所以选择UTF-8不仅仅之于上述原因,我们知道java的就是基于在UTF-8之上的,所以我们选择UTF-8应该没错
首先把我们的.java, .jsp文件都用UTF-8编码来保存,如果以前的没有用UTF-8保存也无所谓,但是建议以后写的都用UTF-8来保存。

      并在.jsp里面写:

以下是引用片段:
<%@page contentType="text/html; charset=UTF-8"%>而不是%@page contentType="text/html; charset=UTF-8"%
      然后在web.xml添加下面一段:

以下是引用片段:
<web-app>
...
  <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
...
</web-app>
      其中com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter的代码如下:

package com.redv.projects.eduadmin.util.filters;
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;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SetCharacterEncodingFilter
    implements Filter {

  protected String encoding = null;
  protected FilterConfig filterConfig = null;
  protected boolean ignore = true;
  public void destroy() {
    this.encoding = null;
    this.filterConfig = null;
  }
  public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {
    // Conditionally select and set the character encoding to be used
    if (ignore || (request.getCharacterEncoding() == null)) {
      String encoding = selectEncoding(request);
      if (encoding != null) {
        request.setCharacterEncoding(encoding);           //
Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
      }
    }
    // Pass control on to the next filter
    chain.doFilter(request, response);
  }
  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding");
    String value = filterConfig.getInitParameter("ignore");
    if (value == null) {
      this.ignore = true;
    }
    else if (value.equalsIgnoreCase("true")) {
      this.ignore = true;
    }
    else if (value.equalsIgnoreCase("yes")) {
      this.ignore = true;
    }
    else {
      this.ignore = false;
    }
  }
  protected String selectEncoding(ServletRequest request) {
    return (this.encoding);
  }
}

      这样,我们的request请求就是以UTT-8编码的,在JSP程序中就可以使用:request.getParameter("myKey")来直接得到UTF-8编码的字符串了,而不需要像这样:new String(request.getParameter("myKey").getBytes("ISO-8859-1"), "GBK")来解决那些乱码了。


stella 上一页12 下一页


精彩推荐
热门文章
热门图文
  • ASP技巧:用isapi_rewrite编写正则表达式
    ASP技巧:用isapi_rew
  • ASP与Flash8联合打造简单新闻评论系统
    ASP与Flash8联合打造
推荐文章
    最新图文
    • 怎样在xp下配置iis
      怎样在xp下配置iis
    • 2000/XP IIS配置问题
      2000/XP IIS配置问题
    最新文章
    lady
    感谢您对点点通的支持  信箱投稿  给点点通提意见
    关于我们 - 广告服务 - 成功客户 - 合作媒体 - 网站历史 - 联系我们 - 免责声明
    Copyright © 2001 - 2008 All Rights Reserved
    苏ICP备08107532号