[Java] to solve the problem in the Chinese post / get requests garbled

The main distortion is caused by non-uniform coding.

In Tomcat8.0 it has solved the problem of Chinese garbled incoming request time for us to get in, so we need to solve the garbage problem in Chinese about the post request parameters.

Directly on Demo

@WebServlet ( "/ Register" )
 public  class RegisterServlet the extends the HttpServlet { 
    @Override 
    protected  void the doPost (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException {
         // setting request can be encoded as utf-8 
        req.setCharacterEncoding ( "utf- . 8 " ); 
        String username = req.getParameter (" username " ); 
        String password = req.getParameter (" password " );
         // set the return format 
        resp.setContentType (" text / HTML; charset = UTF-. 8 " ); 
        resp.getWriter (). println ("注册成功!"+"<br>");
        resp.getWriter().println("username:"+username+"<br>");
        resp.getWriter().println("password:"+password);

    }
}    

 

Guess you like

Origin www.cnblogs.com/robotpaul/p/12500037.html