分类: Java
2008-04-09 09:16:06
问题描述:
1、 表单提交的数据,用request.getParameter(“xxx”)返回的字符串为乱码或者??
2 、直接通过url如中国,这样的get请求在服务端用request. getParameter(“name”)时返回的是乱码;按tomcat4的做法设置Filter也没有用或者用request.setCharacterEncoding("GBK");也不管用
原因:
1、 tomcat的j2ee实现对表单提交即post方式提示时处理参数采用缺省的iso-8859-1来处理
2 、tomcat对get方式提交的请求对query-string 处理时采用了和post方法不一样的处理方式。(与tomcat4不一样,所以设置setCharacterEncoding(“gbk”))不起作用。
解决办法:
首先所有的jsp文件都加上:
1 、实现一个Filter.设置处理字符集为GBK。(在tomcat的webapps/servlet-examples目录有一个完整的例子。请参考web.xml和SetCharacterEncodingFilter的配置。)
1)只要把%TOMCAT安装目录%/ webappsservlets-examplesWEB- INFclassesfiltersSetCharacterEncodingFilter.class文件拷到你的webapp目录/filters下,如果没有filters目录,就创建一个。
2)在你的web.xml里加入如下几行:
3)完成.
2 get方式的解决办法
1) 打开tomcat的server.xml文件,找到区块,加入如下一行:
URIEncoding=”GBK”
完整的应如下:
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="GBK"
/>
2)重启tomcat,一切OK。
执行如下jsp页页测试是否成功
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%
String q=request.getParameter("q");
q = q == null? "没有值" : q;
%>
你提交了:
<%=q%>
输入中文:">
">通过get方式提交
测试结果如果你输入文本框或者点超链都会显示:你提交了”中国”,说明成功!!!!!
以下是引用片段: <%@page contentType="text/html; charset=UTF-8"%>而不是 |
以下是引用片段: ... ... |
以下是引用片段: 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); } } |
以下是引用片段: import java.sql.*; Class.forName("org.gjt.mm.mysql.Driver"); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""); pstmt = con.prepareStatement("SELECT f3, f4 FROM tbl1 WHERE f1 = ? AND f2 = ?"); pstmt.setString(1, new String(f1.getBytes("GBK"), "ISO-8859-1"); pstmt.setString(2, new String(f2.getBytes("GBK"), "ISO-8859-1"); rs = pstmt.executeQuery(); String f3, f4; while(rs.next()) { f3 = new String(rs.getString(1).getBytes("ISO-8859-1"), "GBK"); f4 = new String(rs.getString(2).getBytes("ISO-8859-1"), "GBK"); } } finally { //close resouces ... } |
以下是引用片段: import java.sql.*; import com.redv.sql.encoding.*; Class.forName("org.gjt.mm.mysql.Driver"); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""); //接管数据库连接实例 boolean coding = true; EncodingConnection codingConnection = new EncodingConnection(con, coding, "ISO-8859-1", "GBK"); //获得接管后的数据库连接实例,以后直接使用con已经是经过EncodingConnection重新包装过的实例 con = codingConnection.getConnection(); pstmt = con.prepareStatement("SELECT f3, f4 FROM tbl1 WHERE f1 = ? AND f2 = ?"); pstmt.setString(1, f1); pstmt.setString(2, f2); rs = pstmt.executeQuery(); String f3, f4; while(rs.next()) { f3 = rs.getString(1); f4 = rs.getString(2); } } finally { //close resouces ... } |