当前位置: 代码迷 >> Java Web开发 >> 编码有关问题,为什么其它设遍都不可以,非要设置tomcat的编码才可以
  详细解决方案

编码有关问题,为什么其它设遍都不可以,非要设置tomcat的编码才可以

热度:3825   发布时间:2016-04-10 22:51:25.0
编码问题,为什么其它设遍都不可以,非要设置tomcat的编码才可以?
普通网页提交


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>showMerchandise.htm</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>

<body>

<center>商品显示页,请选购您喜欢的商品</center>

<table width="424" height="583" border="0" align="center">

<tr>
<td width="190"><img src="images/hppavilion.jpg" width="150" height="150" /></td>
<td width="224"><img src="images/lenovo.jpg" width="150" height="150" /></td>
</tr>
<tr>
<td>HP笔记本电脑</td>
<td>移动硬盘 </td>
</tr>
<tr>
<td>价格:5999.00元</td>
<td>价格:700.00元</td>
</tr>
<tr>
<!-- 传递“HP笔记本电脑”的参数 -->
<td><a href="/onlineshoppingcart/servlet/addShoppingCart?id=0001&name=HP笔记本电脑&price=5999.00"><img src="images/buybutton.gif" width="71" height="21" border="0" /></a></td>
<!-- 传递“移动硬盘”的参数-->
<td><a href="/onlineshoppingcart/servlet/addShoppingCart?id=0002&name=移动硬盘&price=700.00"><img src="images/buybutton.gif" width="71" height="21" border="0" /></a></td>
</tr>

<tr>
<td><img src="images/mouse.jpg" width="200" height="200" /></td>
<td><img src="images/lcd.jpg" width="150" height="150" /></td>
</tr>
<tr>
<td>鼠标</td>
<td>LCD显示器</td>
</tr>
<tr>
<td>价格:80.00元</td>
<td>价格:1999.00元</td>
</tr>
<tr>
<!-- 传递“鼠标”的参数-->
<td><a href="/onlineshoppingcart/servlet/addShoppingCart?id=0003&name=鼠标&price=80.00"><img src="images/buybutton.gif" width="71" height="21" border="0" /></a></td>
<!-- 传递“LCD显示器”的参数-->
<td><a href="/onlineshoppingcart/servlet/addShoppingCart?id=0004&name=LCD显示器&price=1999.00"><img src="images/buybutton.gif" width="71" height="21" border="0" /></a></td>
</tr>

<tr>
<td><img src="images/learning.jpg" width="150" height="150" /></td>
<td><img src="images/game.jpg" width="200" height="200" /></td>
</tr>
<tr>
<td>电子词典</td>
<td>掌上游戏机</td>
</tr>
<tr>
<td>价格:438.00元</td>
<td>价格:¥638.00 元</td>
</tr>
<tr>
<!-- 传递“电子词典”的参数-->
<td><a href="/onlineshoppingcart/servlet/addShoppingCart?id=0005&name=电子词典&price=438.00"><img src="images/buybutton.gif" width="71" height="21" border="0" /></a></td>
<!-- 传递“掌上游戏机”的参数-->
<td><a href="/onlineshoppingcart/servlet/addShoppingCart?id=0006&name=掌上游戏机&price=638.00"><img src="images/buybutton.gif" width="71" height="21" border="0" /></a></td>
</tr>

</table>

</body>
</html>



get提交
/onlineshoppingcart/servlet/addShoppingCart?id=0001&name=HP笔记本电脑&price=5999.00


public class AddShoppingCartSevlet extends HttpServlet
{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExceptionIOException
{
String name = request.getParameter("name");
System.out.println(name);

request.setCharacterEncoding("utf-8");
System.out.println(request.getParameter("name"));

doPost(request, response);
}
}


先不说下面的业务处理了,这里打印出来的name就是乱码;

很搞不懂为什么,项目是utf-8编码
html是,request没设置和设置后打印出来也一样。

不晓得为什么get方式提交后出现这样的乱码。






自己寻得的方法,在tomcat的server.xml设置下

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"redirectPort="8443"  
           URIEncoding="UTF-8" /> 

  <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" 
          URIEncoding="UTF-8"/>

加上编码,就正常显示了




在修改tomcat配置前,自己还尝试了下过滤器,过滤内容如下,但是也不行

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.yulei.filter.EncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
  相关解决方案