req.setCharacterEncoding("GBK")怎么不起作用?
我的servlet中:
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding( "GBK ");
String fromAccount = req.getParameter( "from ");
resp.setContentType( "text/html; charset=GBK ");
java.io.PrintWriter out = resp.getWriter();
out.println(fromAccount);
out.println( "你好 ");
}
我已经设置接收参数的编码格式req.setCharacterEncoding( "GBK ");但我用
./WebTest?from=谢谢
调用则显示是乱码,而out.println( "你好 ");则可以正常显示
除非我在程序中再转换一次才能显示中文
fromAccount = new String(fromAccount.getBytes( "ISO-8859-1 "), "GBK ");
难道req.setCharacterEncoding( "GBK ");不起作用么?
[解决办法]
JSP页面编码改GBK试试?
[解决办法]
在TOMCAT5 中setCharacterEncoding好像只能对POST方式传送的参数有效!TOMCAT5中对POST和GET是使用不同的方式处理的,你需要对TOMCAT进行配置或者自己进行转码。