当前位置: 代码迷 >> Java Web开发 >> jsp参数传递乱码解决思路
  详细解决方案

jsp参数传递乱码解决思路

热度:7710   发布时间:2013-02-25 21:12:47.0
jsp参数传递乱码
代码如下:
《jsp文件》
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%request.setCharacterEncoding("GBK");%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title></title>
  </head>
  <body>
  <div>
  <form method="post" name="searchform" action="SearchServlet">
  <font face="华文彩云" size="6" style="position:relative;top:6px;">站内搜索</font> 
  <input type="text" name="SearchContent" value="请输入关键字" 
  style="font-size:18px;width:150;height:35; position:relative;top:10px;left:5px;bottom:10px;"
  onmouseover=this.focus();this.select();
  onclick="if(value==defaultValue){value='';this.style.color='#000'}"
  onBlur="if(!value){value=defaultValue;this.style.color='#999'}" />
  <input type="submit" value="" style="background:url(image/find.png);width:35;height:35;position:relative;top:10px;left:5px;">
  </form>  
  </div>
</body>
</html>
《servlet.java文件》
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String SearchContent = new String(request.getParameter("SearchContent").getBytes("ISO-8859-1"),"GBK");
//String SearchContent1 = URLDecoder.decode(request.getParameter("SearchContent"), "GBK");
System.out.println(SearchContent);
}
代码如上所示,但是到最后在控制台打印出来的结果总是乱码,有哪位高手帮忙解决一下,调了一天了,网上的解决办法也看了很多,还是不行。。解决办法不能涉及到修改tomcat的配置文件。。

------解决方案--------------------------------------------------------
在 html 的 head里面加上


<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>

这样在浏览器提交表单的时候会把数据进行gbk编码

------解决方案--------------------------------------------------------
new String(request.getParameter("typename").getBytes("ISO-8859-1"),"utf-8")试试
jsp页面改成utf-8编码
------解决方案--------------------------------------------------------
探讨

在 html 的 head里面加上


<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>

这样在浏览器提交表单的时候会把数据进行gbk编码

------解决方案--------------------------------------------------------
楼上的,应该可以的。
------解决方案--------------------------------------------------------
jsp 头部改成 pageEncoding="UTF-8"

另外你这里是post提交,代码怎么粘的
public void doGet(HttpServletRequest request, HttpServletResponse response)

是粘错了,还是实际代码就是错的,在doPost方法里处理post提交。
------解决方案--------------------------------------------------------
Java code
 //在doPost里面  这个是针对POST提交的request.request.setCharacterEncoding("GBK");/*这是针对GET方式提交的String SearchContent = new String(request.getParameter("SearchContent").getBytes("ISO-8859-1"),"GBK");*/
------解决方案--------------------------------------------------------
把响应格式也设置成GBK格式。
  相关解决方案