⑴ 解決servlet中遇到的亂碼問題
情況一:
在輸出的時候遇到的亂碼:
out.println("<html><head><title></title><body>HelloWorld!你好!</body></head></html>");//“你好“顯示的是亂碼。
解決方法:
resp.setContentType(“text/html;charset=GBK”);
情況二:
在web.xml裏配置相關的servletConfig信息時候遇到的亂碼:
<init-param>
<param-name>name</param-name>
<param-value>逸龍</param-value>
</init-param>
解決方法:
String s = this.getServletConfig().getInitParameter("name");
s = new String(s.getBytes("iso8859-1"), "GBK");
注:ios8859-1每次輸出一個字節,而漢字佔用了兩個字節。可以將iso8859-1看待的字節按照GBK格式形成的字符串賦給s
如果還不行,則有可能是tomcat版本按utf-8進行編碼的,可以嘗試將web.xml裏面的encoding="ISO-8859-1"改成encoding=”GBK”,然後通過:
S=this.getServletConfig().getInitParameter(“name”);得到該按utf-8編碼的字符串。然後用s=new String(s.getBytes(“utf-8”),GBK);將原本按utf-8編碼的字符串按GBK形式賦給s,然後再輸出。
⑵ 解決JSP中遇到的亂碼問題:
情況一:HTML文件向JSP提交請求時遇到的亂碼問題
文件a1.html:
<%!以get方式提交%>
<form action=b.jsp method=get>
<input name=user value=我們>
<input type=submit value=get>
</form>
<%!以post方式提交%>
<form action=b.jsp method=post>
<input name=user value=他們>
<input type=submit value=post>
</form>
<%!以連接方式提交%>
<a href="b.jsp?user=你們">這是一個連接</a>
文件b.jsp:
<%@ page contentType="text/html;charset=GBK"%>
<%request.setCharacterEncoding("GBK");%>
<%=request.getParameter("user")%>
說明:b.jsp的第一句是解決get方式和鏈接方式下的亂碼問題,第二句則是解決post方式下提交而引起的亂碼問題。
注意:在這種情況下,提交所在的HTML頁面也可能會由於中文(比如說“我們”的顯示)而導致出現亂碼問題,對於此,可以嘗試在文件頭部加上:
<%@ page contentType=”text/html;charset=GBK”%>
情況二:JSP頁面向JSP頁面提交請求時遇到的亂碼問題
文件a2.jsp(其內容同a1.html,不同的地方在於這個文件最好不要加入:
<%@ page contentType=”text/html;charset=GBK”%>)
文件b.jsp(同上)
說明:這種情況基本跟情況一相同。
情況三:JSP頁面以param動作指令向另一個JSP頁面提交請求時出現的亂碼問題
文件a5.jsp:
<html>
<body>
<%
String s = null;
s = "你好";
%>
<jsp:forward page="forParam.jsp">
<jsp:param name="s" value="<%=s%>"/>
</jsp:forward>
</body>
</html>
文件forParam.jsp:
<%@ page contentType="text/html;charset=GBK"%>
<html>
<body>
<%
request.setCharacterEncoding("GBK");
String tempString = request.getParameter("s");
out.println("接收到的參數s爲:" + tempString);
%>
</body>
</html>
需要注意的是:這種情況下最好就不要再在提交的那個頁面,也就是a5.jsp裏面加入
<%@ page contentType="text/html;charset=GBK">
否則可能會報錯。另外,上面兩句代碼必須都要出現,少了哪句都不行。
還有一種處理的方法是:
文件b1.jsp:
<%@ page contentType="text/html;charset=GBK">
<html>
<body>
<%
String tempString = request.getParameter("s");
byte tempB[] = tempString.getBytes("ISO-8859-1");
tempString = new String(tempB);
out.println("接受到的字符串爲:" + tempString);
%>
</body>
</html>
(3)以form表單的方式提交
l 儘量以post方式提交
l 接收端如果是JSP或者Servlet,則可以使用語句
request.setCharacterEncoding(“GBK”);
l 如果使用的是struts2,則需在struts.xml文件中加入配置:
<constant name="struts.i18n.encoding" value="GBK" />
或者在web.xml文件中加入配置(Spring):
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
說明:必須放在struts2配置前纔會起作用。
以連接的方式提交
l <%@page contentType=”text/html;charset=GBK”%>
l String myName = request.getParamter(“name”);
myName = new String(myName.getBytes(“ISO8859-1”), “GBK”);