首先對於源jsp網站和servlet里面的字符集要一樣,一般支持中文的字符集為UTF-8最好采用這個字符集(除此之外還有gb2312);
對於源jsp文件的代碼中需要設置
設置你的page里面的字符集
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
設置html文件里面的字符集
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
如果對於參數通過get方法進行傳參的話,有兩種方法:
方法一:
在tomcat里面設置字符集為UTF-8
修改server.xml
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>
* 必須有修改tomcat服務器配置文件權限
方法二:通過對servlet里面的進行設置:
response.setHeader("Content-Type", "text/html;charset=UTF-8");//設置UTF-8的顯示頁面的類型和字符集
username = new String(username.getBytes("ISO8859-1"),"utf-8");//根據你的tomcat里面的字符集進行對中文進行轉換,將iso轉換為UTF-8
如果對於參數通過post方法進行傳參的話,則設置為:
response.setHeader("Content-Type", "text/html;charset=UTF-8");//設置UTF-8的顯示頁面的類型和字符集
request.setCharacterEncoding("utf-8"); //設置通過post方法進行傳參的字符集
在這里需要注意的是:
1.對於post和get傳參的時候解決中文亂碼問題的時候一定要分清楚是哪種方式傳參,是post就得用post不然會沒用的
2.對於兩種不同的地方不能合用,亂碼問題解決不了的。
下面附上一個例子:
get.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>
<form action="servlet/test" method="get">
<input type="text" name="getV">
<button type="submit">測試get請求</button>
</form>
</body>
</body>
</html>
post.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>
<form action="servlet/test" method="post">
<input type="text" name="postV">
<button type="submit">測試post請求</button>
</form>
</body>
</html>
test.java
package testcharset;
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 test extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Content-Type", "text/html;charset=UTF-8");//設置UTF-8的顯示頁面的類型和字符集
PrintWriter out=response.getWriter();
try {
String username=request.getParameter("getV");
username = new String(username.getBytes("ISO8859-1"),"utf-8");//根據你的tomcat里面的字符集進行對中文進行轉換,將iso轉換為UTF-8
out.print("獲取的get請求:");
out.print(username);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
out.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Content-Type", "text/html;charset=UTF-8");//設置UTF-8的顯示頁面的類型和字符集
request.setCharacterEncoding("utf-8"); //設置通過post方法進行傳參的字符集
PrintWriter out=response.getWriter();
try {
out.print("獲取的Post請求:");
out.print(request.getParameter("postV"));
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
out.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
}
}
