最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

解决JSP中使用request乱码问题

日期:12-25    来源:中国设计秀    作者:cnwebshow.com

经常在讨论区看到有人问我的jsp显示中文有乱码怎么办,我用request得到的用户输入的中文怎么是乱码,我把汉字写到数据库怎么是乱码,等等一些关于汉字乱码的问题。vwv中国设计秀
其实这个问题很简单,管它汉字不汉字,还是日文,还是其他的什么双字节的语言,我们一律把它当作UTF-8看待。vwv中国设计秀
(一)request中的双字节文字vwv中国设计秀
好下面我们就来实现在整个应用程序中使用UTF-8编码工作,之所以选择UTF-8不仅仅之于上述原因,我们知道java的就是基于在UTF-8之上的,所以我们选择UTF-8应该没错^_^vwv中国设计秀
我们首先把我们的.java, .jsp文件都用UTF-8编码来保存,如果以前的没有用UTF-8保存也无所谓,但是建议以后写的都用UTF-8来保存。vwv中国设计秀
并在.jsp里面写:<%@page contentType="text/html; charset=UTF-8"%>而不是<%@page contentType="text/html; charset=UTF-8"%>vwv中国设计秀
然后在web.xml添加下面一段:vwv中国设计秀
<web-app>vwv中国设计秀
...vwv中国设计秀
  <filter>vwv中国设计秀
    <filter-name>Set Character Encoding</filter-name>vwv中国设计秀
    <filter-class>com.redv.PRojects.eduadmin.util.filters.SetCharacterEncodingFilter</filter-class>vwv中国设计秀
    <init-param>vwv中国设计秀
      <param-name>encoding</param-name>vwv中国设计秀
      <param-value>UTF-8</param-value>vwv中国设计秀
    </init-param>vwv中国设计秀
  </filter>vwv中国设计秀
  <filter-mapping>vwv中国设计秀
    <filter-name>Set Character Encoding</filter-name>vwv中国设计秀
    <url-pattern>/*</url-pattern>vwv中国设计秀
  </filter-mapping>vwv中国设计秀
...vwv中国设计秀
</web-app>vwv中国设计秀

其中com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter的代码如下:vwv中国设计秀

package com.redv.projects.eduadmin.util.filters;vwv中国设计秀

import java.io.IOException;vwv中国设计秀
import javax.servlet.Filter;vwv中国设计秀
import javax.servlet.FilterChain;vwv中国设计秀
import javax.servlet.FilterConfig;vwv中国设计秀
import javax.servlet.ServletException;vwv中国设计秀
import javax.servlet.ServletRequest;vwv中国设计秀
import javax.servlet.ServletResponse;vwv中国设计秀
import javax.servlet.UnavailableException;vwv中国设计秀
import javax.servlet.http.HttpServletRequest;vwv中国设计秀
import javax.servlet.http.HttpServletResponse;vwv中国设计秀

vwv中国设计秀
public class SetCharacterEncodingFiltervwv中国设计秀
    implements Filter {vwv中国设计秀

vwv中国设计秀
  protected String encoding = null;vwv中国设计秀

  protected FilterConfig filterConfig = null;vwv中国设计秀

  protected boolean ignore = true;vwv中国设计秀

  public void destroy() {vwv中国设计秀

    this.encoding = null;vwv中国设计秀
    this.filterConfig = null;vwv中国设计秀

  }vwv中国设计秀

  public void doFilter(ServletRequest request, ServletResponse response,vwv中国设计秀
                       FilterChain chain) throws IOException, ServletException {vwv中国设计秀

    // Conditionally select and set the character encoding to be usedvwv中国设计秀
    if (ignore || (request.getCharacterEncoding() == null)) {vwv中国设计秀
      String encoding = selectEncoding(request);vwv中国设计秀
      if (encoding != null) {vwv中国设计秀
        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().vwv中国设计秀
      }vwv中国设计秀
    }vwv中国设计秀

    // Pass control on to the next filtervwv中国设计秀
    chain.doFilter(request, response);vwv中国设计秀

  }vwv中国设计秀

  public void init(FilterConfig filterConfig) throws ServletException {vwv中国设计秀

    this.filterConfig = filterConfig;vwv中国设计秀
    this.encoding = filterConfig.getInitParameter("encoding");vwv中国设计秀
    String value = filterConfig.getInitParameter("ignore");vwv中国设计秀
    if (value == null) {vwv中国设计秀
      this.ignore = true;vwv中国设计秀
    }vwv中国设计秀
    else if (value.equalsIgnoreCase("true")) {vwv中国设计秀
      this.ignore = true;vwv中国设计秀
    }vwv中国设计秀
    else if (value.equalsIgnoreCase("yes")) {vwv中国设计秀
      this.ignore = true;vwv中国设计秀
    }vwv中国设计秀
    else {vwv中国设计秀
      this.ignore = false;vwv中国设计秀
    }vwv中国设计秀

  }vwv中国设计秀

  protected String selectEncoding(ServletRequest request) {vwv中国设计秀

    return (this.encoding);vwv中国设计秀

  }vwv中国设计秀

}vwv中国设计秀

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

(二)数据库处理的双字节文字 http://www.upas.org/java/DatabaseEncodingProblemSolution/vwv中国设计秀
另外一个,就是写入数据库的问题,我们知道我们在使用MySQL的时候可以改用这样的url来处理汉字编码问题:jdbc:mysql://localhost:3306/upas?useUnicode=true&characterEncoding=gb2312,vwv中国设计秀
那么对于那些我们无法像mysql这样解决的怎么办呢?难道我们每次都这样写吗:vwv中国设计秀
import java.sql.*;vwv中国设计秀

Class.forName("org.gjt.mm.mysql.Driver");vwv中国设计秀
Connection con = null;vwv中国设计秀
PreparedStatement pstmt = null;vwv中国设计秀
ResultSet rs = null;vwv中国设计秀
try {vwv中国设计秀
  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");vwv中国设计秀
  pstmt = con.prepareStatement("SELECT f3, f4 FROM tbl1 WHERE f1 = ? AND f2 = ?");vwv中国设计秀
  pstmt.setString(1, new String(f1.getBytes("GBK"), "ISO-8859-1");vwv中国设计秀
  pstmt.setString(2, new String(f2.getBytes("GBK"), "ISO-8859-1");vwv中国设计秀
  rs = pstmt.executeQuery();vwv中国设计秀
  String f3, f4;vwv中国设计秀
  while(rs.next()) {vwv中国设计秀
    f3 = new String(rs.getString(1).getBytes("ISO-8859-1"), "GBK");vwv中国设计秀
    f4 = new String(rs.getString(2).getBytes("ISO-8859-1"), "GBK");vwv中国设计秀
  }vwv中国设计秀
}vwv中国设计秀
finally {vwv中国设计秀
  //close resoucesvwv中国设计秀
  ...vwv中国设计秀
}vwv中国设计秀

其实我们完全可以这样写: vwv中国设计秀
import java.sql.*;vwv中国设计秀
import com.redv.sql.encoding.*;vwv中国设计秀

Class.forName("org.gjt.mm.mysql.Driver");vwv中国设计秀
Connection con = null;vwv中国设计秀
PreparedStatement pstmt = null;vwv中国设计秀
ResultSet rs = null;vwv中国设计秀
try {vwv中国设计秀
  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");vwv中国设计秀
  //接管数据库连接实例vwv中国设计秀
  boolean coding = true;vwv中国设计秀
  EncodingConnection codingConnection = new EncodingConnection(con, coding, "ISO-8859-1", "GBK");vwv中国设计秀
  //获得接管后的数据库连接实例,以后直接使用con已经是经过EncodingConnection重新包装过的实例vwv中国设计秀
  con = codingConnection.getConnection();vwv中国设计秀
  pstmt = con.prepareStatement("SELECT f3, f4 FROM tbl1 WHERE f1 = ? AND f2 = ?");vwv中国设计秀
  pstmt.setString(1, f1);vwv中国设计秀
  pstmt.setString(2, f2);vwv中国设计秀
  rs = pstmt.executeQuery();vwv中国设计秀
  String f3, f4;vwv中国设计秀
  while(rs.next()) {vwv中国设计秀
    f3 = rs.getString(1);vwv中国设计秀
    f4 = rs.getString(2);vwv中国设计秀
  }vwv中国设计秀
}vwv中国设计秀
finally {vwv中国设计秀
  //close resoucesvwv中国设计秀
  ...vwv中国设计秀
}vwv中国设计秀

看看,怎么样,我们只需要在获取数据库连接的地方稍微修改一下,甚至我们可以把它当作参数保存在 properties里面,改变coding的布尔值来设定是否使用自动编码转换。常常我们可以使用一个Database类来封装获取数据库连接的那段getConnection,以便于我们可以从 javax.sql.DataSource中获取到数据库连接。这个时候我们仅仅需要修改我们的Database类即可,而不用去搜索所有使用了rs.setString(), rs.getString()的地方去加入我们的编码转换代码了。甚至我们在使用con.createStatment()语句时,即使我们sql语句含有汉字或者其它的双字节字符时一样没有问题: vwv中国设计秀
SELECT 姓名, 性别 FROM 学生表 WHERE 班级 LIKE '%计算机%'vwv中国设计秀

本文引用地址:/bc/article_46610.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明