浏览器中输入地址 –> 浏览器对中文进行编码 –>发送到服务器 ->服务器进行解码
如果浏览器编码和服务器解码用的字符集不一致就会发生乱码问题。
乱码解决方式:
浏览器访问
浏览器默认使用UTF-8编码(IE默认GBK当然可以通过meta标签设置)
<meta http-equiv="content-type" content="text/html; charset=gbk"/>
服务器(Tomcat)默认使用iso-8859-1解码。iso-8859-1是不支持中文的,也就是说不做处理,中文是一定乱码的。
Servlet代码处理:
String user_Name = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
GET方式解决:
单纯设置request.setCharacterEncoding("UTF-8");是没有用的,所以我们把默认的iso-8859-1编码改成UTF-8,在TOMCAT的配置文件的server.xml中更改:
<Connector port="8080"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
添加URIEncoding=UTF-8
POST方式解决:
比如表单提交,在Servlet中设置request.setCharacterEncoding("UTF-8");就能很好的解决。
在servlet设置响应编码
resp.setContentType("text/html;charset=utf-8");//设置响应编码
实例如下:web.xml
<?xml version="1.0" encoding="utf-8"?><!--utf-8不区分大小写-->
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--login.html与WEB-INF是同级文件,在WebRoot建立-->
<!--注册servlet-->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>cn.loginservlet.LoginServlet</servlet-class><!--包名加类名-->
</servlet>
<!--映射-->
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginservlet</url-pattern>
</servlet-mapping>
<!--注册login.html 浏览器登录:http://192.168.5.7:8080/login/login.html-->
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>
java代码:
package cn.loginservlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");//解决post方法请求乱码问题
resp.setContentType("text/html;charset=utf-8");//设置响应编码
System.out.println("loginservlet doGet......");
String userName = req.getParameter("userName");
String passWord = req.getParameter("passWord");
System.out.println("userName:"+userName+"\npassWord:"+passWord);
/**解决get方法的乱码问题 :在Tomact server.xml文件中更改
* <Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"(添加这一句就好了)
/> */
//方式2(局部):
//String user_name = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
//System.out.println("userName 用get方法解决乱码后:"+user_name+"\npassWord:"+passWord);
PrintWriter pw = resp.getWriter();
if(userName.equals("admin")&&passWord.equals("admin")){
pw.println("<h1>login is sucess成功</h1> 成功");//可以学标签
}else{
pw.println("<h1>login is fail失败</h1> 失败");
}
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("LoginServlet doPost >>>>");
doGet(req,resp);
}
}
login.html代码:
<html> <head> <title>title</title> <!--设置客户端浏览器编码--> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <link rel="stylesheet" type="text/css" href="css/mystyle.css"></link> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/myscript.js"></script> </head> <body> <div> <div id="main"> <h2>登录</h2> <!--method方法为get 执行LoginServlet中doGet方法 post执行doPost方法--> <form action="./loginservlet" method="post" name="loginForm"> 用户名:<input type="text" name="userName"/><br/> 密 码:<input type="password" name="passWord"/><br/> <button>提交</button> </form> </div> </div> </body> </html>
图从左到右: 图1:局部解决get方法乱码问题 图2:解决get方法乱码问题后 图3:post方法未解决乱码 图4:解决post方法请求乱码问题 图5:更改响应乱码前 图6:解决响应乱码后