search

Home  >  Q&A  >  body text

java - servlet中,用浏览器打印HTTP表单,出现乱码的问题,下面是相关的代码

用chrome 和 IE都试过(也把编码换成 UTF-8),网址输入为 :
http://localhost:8080/mydoodling/info?username=张
动态生成的网页内容为: 你好寮?
请问中文代码到底是哪个?
获取浏览器表单参数的代码为:

public class ServletInfo extends HttpServlet        
{
        public void doGet ( HttpServletRequest req , HttpServletResponse resp) throws IOException 
        {
                String username = req . getParameter ( "username") ;
                if (null != username)
                {
                        username = new String ( username.getBytes ( "ISO-8859-1")  , "GB2312") ;
                }
                resp . setContentType ( "text/html;charset=GB2312") ; 
                PrintWriter out = resp . getWriter ( ) ;
                out . println ("<html><body>"+"你好" + username+"</body></html>") ; 
                out . close ( ) ;
        }                
}
巴扎黑巴扎黑2889 days ago287

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:50:05

    1. Check the request.setCharacterEncoding() setting or the character encoding filter in the filter to keep the decoding format consistent with the set encoding format

    2. For GET requests Tomcat will use ISO-8859-1 encoding and then UTF-8 encoding

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:50:05

    Check the character encoding on the jsp page

    reply
    0
  • PHPz

    PHPz2017-04-17 17:50:05

    Typical URLs are garbled in Chinese, choose one below.
    1. Add URIEncoding="UTF-8" to the <Connector> tag in tomcat's configuration file server.xml
    2. Add request.setCharacterEncoding("UTF-8") in the first line of the method

    reply
    0
  • Cancelreply