或许很多人会出现??这种令人头痛的mysql的中文乱码问题:解决如下:
1.先对于新建的数据库要设置默认的字符集为UTF-8
create database mydb default character set utf8 collate utf8_general_ci;
2.然后对于想对应的表进行设置默认字符集
CREATE TABLE IF NOT EXISTS `mydb` ( `username` varchar(64) NOT NULL, `userid` int(11) NOT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.最重要的一步在jdbc中的uri的地址中添加如下语句?characterEncoding=UTF-8设置字符集为UTF-8
jdbc:mysql://localhost:3306/db_name?characterEncoding=UTF-8
如下是一个例子:
regServlet.java
package cn.lonecloud.demo; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class RegServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Connection conn=null; PreparedStatement prst=null; ResultSet rs=null; String db_driver="com.mysql.jdbc.Driver"; String db_url="jdbc:mysql://localhost:3306/db_friend?characterEncoding=UTF-8"; String db_user="root"; String db_password=""; String sql1="select * from userinfo where username=?"; String sql2="insert into userinfo values(?,?)"; try { String username=request.getParameter("userName"); String userpass=request.getParameter("password"); Class.forName(db_driver); conn=DriverManager.getConnection(db_url,db_user,db_password); prst=conn.prepareStatement(sql1); prst.setString(1, username); rs=prst.executeQuery(); if (!rs.next()) { prst=conn.prepareStatement(sql2); prst.setString(1, username); prst.setString(2, userpass); int count=prst.executeUpdate(); if (count!=0) { HttpSession session=request.getSession(); session.setAttribute("username", username); request.setAttribute("msg", "注册成功!"); request.getRequestDispatcher("index.jsp").forward(request, response); }else{ request.setAttribute("msg", "登录失败,密码错误!"); request.getRequestDispatcher("error.jsp").forward(request, response); } }else { request.setAttribute("msg", "用户不存在!"); request.getRequestDispatcher("error.jsp").forward(request, response); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ if (rs!=null) { try { rs.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } if (prst!=null) { try { prst.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } if (conn!=null) { try { conn.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } out.close(); } } }
reg.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>新用户注册</h1>
<form action="RegServlet" method="post">
<input type="text" name="userName"></br>
<input type="password" name="password"></br>
<input type="submit" id="btn_load">
</form>
</body>
</html>
LoginFilter.java
package cn.lonecloud.demo; 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.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //在过滤器中必须加入下面这两句话,不然会不能这样调用下面的方法 HttpServletRequest req=(HttpServletRequest) request; HttpServletResponse resp=(HttpServletResponse)response; request.setCharacterEncoding("UTF-8"); String path=req.getServletPath(); HttpSession session=req.getSession(); String username=(String)session.getAttribute("username"); System.out.println(path); boolean b1=path.endsWith("login.jsp"); boolean b2=path.endsWith("error.jsp"); boolean b3=path.endsWith("LoginServlet"); boolean b4=path.endsWith("RegServlet"); boolean b5=path.endsWith("reg.jsp"); boolean b6=(username!=null); if (b1||b2||b3||b4||b5||b6) { chain.doFilter(request, response); }else { resp.sendRedirect("error.jsp"); } } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }