现在的位置: 首页 > 综合 > 正文

Java Servlet输出的中文乱码问题

2013年12月29日 ⁄ 综合 ⁄ 共 307字 ⁄ 字号 评论关闭

问题如上一篇博文中,如果在HelloYourName.html中输入中文将会返回乱码。

解决办法如下:

1.在获取out之前先设置格式

res.setContentType("text/html; charset=GB2312");

res.setCharacterEncoding("GB2312");

2.获取参数时,把它转成字节码的形式再转成GB2312格式

String name;

try

{

    name = new String(req.getParameter("name").getBytes("ISO-8859-1"),"gb2312");

}

catch(NullPointerException e)

{

    name = "";

}

抱歉!评论已关闭.