这次我写了过滤码,,但是还是在存入mysql数据库时是乱码
过滤器源码是:
package TR;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;/**
*
* Date: 2005-9-19
* Time: 17:33:36
* @author Duzk
*/
public class MyFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} //Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) { try {
request.setCharacterEncoding("GBK");
filterChain.doFilter(request, response); } catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
} //Clean up resources
public void destroy() {
}
}然后在web.xml里加上这段
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>TR.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>而我用来测试的jsp源码为
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ page import="java.sql.*" %>
<html>
<meta http-equiv="Content-Type" content="text/html;charset=GBK">
<body><form name="form1" method="post" action="">
<%
Connection conn=null;
Statement sql=null;
ResultSet rs=null;
request.setCharacterEncoding("GBK"); // String abc = new String(request.getParameter("textfield").getBytes("ISO-8859-1"));
String name = request.getParameter("textfield");
//name=new String(name.getBytes("ISO-8859-1"), "bg2312");
out.println(name);
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (ClassNotFoundException e){}
try
{
String url="jdbc:mysql://localhost:3306/english" ; conn=DriverManager.getConnection(url,"root","123456");
sql=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs=sql.executeQuery("Select * from text");
sql.executeUpdate("insert into text values"+"("+"'"+name+"')");
}
catch (Exception e) {}
%>
<label>
<input type="text" name="textfield">
</label>
<label>
<input type="submit" name="Submit" value="提交">
</label>
</form>
</body>
</html>
但还是不行??请救救,,应该如何改啊?
过滤器源码是:
package TR;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;/**
*
* Date: 2005-9-19
* Time: 17:33:36
* @author Duzk
*/
public class MyFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} //Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) { try {
request.setCharacterEncoding("GBK");
filterChain.doFilter(request, response); } catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
} //Clean up resources
public void destroy() {
}
}然后在web.xml里加上这段
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>TR.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>而我用来测试的jsp源码为
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ page import="java.sql.*" %>
<html>
<meta http-equiv="Content-Type" content="text/html;charset=GBK">
<body><form name="form1" method="post" action="">
<%
Connection conn=null;
Statement sql=null;
ResultSet rs=null;
request.setCharacterEncoding("GBK"); // String abc = new String(request.getParameter("textfield").getBytes("ISO-8859-1"));
String name = request.getParameter("textfield");
//name=new String(name.getBytes("ISO-8859-1"), "bg2312");
out.println(name);
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (ClassNotFoundException e){}
try
{
String url="jdbc:mysql://localhost:3306/english" ; conn=DriverManager.getConnection(url,"root","123456");
sql=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs=sql.executeQuery("Select * from text");
sql.executeUpdate("insert into text values"+"("+"'"+name+"')");
}
catch (Exception e) {}
%>
<label>
<input type="text" name="textfield">
</label>
<label>
<input type="submit" name="Submit" value="提交">
</label>
</form>
</body>
</html>
但还是不行??请救救,,应该如何改啊?
设置你的mysql的编码方式为utf8
CREATE TABLE xx` (... ) ENGINE=MyISAM DEFAULT CHARSET=gbk;String url="jdbc:mysql://localhost:3306/english" ;
改为String url="jdbc:mysql://localhost:3306/english?useUnicode=TRUE&characterEncoding=GBK";
--default-characte-set=gbk
然后向数据库写数据的时候,用GBK转换一下字符串,如果不设置数据库的编码格式,很多时候都不能成功。
String tran(String tem)
{
String str=null;
try
{
byte b[]=tem.getBytes("ISO-8859-1");
str=new String(b);
}
catch(Exception e){}
return str;
}%>
我在SQL中是用这个函数来过虑的..
String name = request.getParameter("textfield");
if(name!=null)
name=tran(name);
else
name="";
设置数据库编码为GBK或者gb2312
设置GET方式传递参数编码为GBK或者gb2312,在serverl.xml中你的服务中设置URIEncoding="GBK"
设置POST方式传递参数编码为GBK或者gb2312,在页面或者过滤器中设置request.setCharacterEncoding("GBK");
这样以后中文就随我怎么用了,再也没有讨厌的中文问题了。
xiangbo520(充栋汗牛)的方法我明天试试,,行了就立刻结贴,,再次谢谢大家的讨论,,可以的话再提供点好些的方法..谢谢大家了..
[email protected]
我想问一下configration这个怎么配置呢??可以祥细一点说呢??谢谢 了..