问题如上一篇博文中,如果在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 = "";
}