⑴ 解决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”);