servlet解决中文乱编的问题(5)
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</center>
<pre>
当把中文做为参数进行传递的时候,容易产生中文乱码问题,可采用如下方式解决:
POST传递:request.setCharacterEncoding("GBK");
GET传递:
将接收过来的参数进行重新转码
String name=request.getParameter("username");
name=new String(name.getBytes("ISO8859-1"),"GBK");
修改server.xml
URIEncoding="GBK"
</pre>
</body>
</html>
[html]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>encoding</servlet-name>
<servlet-class>com.mars.check</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>encoding</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>encoding</servlet-name>
<servlet-class>com.mars.check</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>encoding</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
</web-app>
[java]
package com.mars;
相关新闻>>
- 发表评论
-
- 最新评论 更多>>