建站部落网 - www.jzbulo.com 首 页业界动态免费空间
建站首页 | 业界动态 | 网页制作 | 网络编程 | 网站运营 | 服务器技术 | 网页特效 | 图形图像 | 计算机技术 | 作品欣赏
您当前的位置:建站部落 > 网络编程 > JSP > 文章内容 注册用户 用户管理 退出登录
解决JSP中使用request乱码问题
作者:佚名  来源:不详  发布时间:2007-11-24 14:45:33  发布人:jzbulo

减小字体 增大字体

    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")来解决那些乱码了。

[] [返回上一页] [打 印] [收 藏]
相关文章 赞助商链接
· 写给刚刚踏入网页设计这个世界的你
· DesignUK 设想英伦系列活动之大声展21日在沪..
· 婚恋交友网站盈利模式初探
· CNAP2007国际艺术展在北京酒厂国际艺术园医..
· 德国DFI学院学生获奖作品之一:大众篇(1)
· Xbox Forza Street Racing 平面广告设计
· Jessica Vernick 平面设计
· Kelsey Reckord 平面设计
· PRINT-RUN伦敦防治肺癌慈善海报展欣赏
· [摄影] 水中芙蓉
· [同盟报道]清华美院2007本科毕业展开幕
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图
声明:本站中的内容及图片来均源于互联网,其中演示的技术细节仅用于试验 环境的技术研究以及漏洞的验证。
文章内容不涉及任何有版权的内容,仅供技术交流研究之用。网 站联系QQ:175612638
Copyright © 2002-2006 jzbulo.com. All Rights Reserved . 粤ICP备05040326号