我的myeclpse能设置成UTF-8的地方全设置成UTF-8了,html是这么写的:
<html>  <head>
  
  
  <meta charset="UTF-8">
    <meta name="content-type" content="text/html; charset=UTF-8">
    <title>regist.html</title>

    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  </head>
  
  <body>
    
    <form action="/Practice/RequestDemo1">
     用户名:<input type="text" name="username">
     密码:<input type="password"  name="password">
     验证码:<input type="text"  name="verifycode">
     <input type="submit"  value="提交并注册"> 
    </form>
  </body>
</html>通过request取数据是这么写的:public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
System.out.println("用户名"+request.getParameter("username"));
System.out.println("密码"+request.getParameter("password"));
System.out.println("验证码"+request.getParameter("verifycode"));然后打印到控制台从浏览器那边提交过来的中文就都是乱码了

解决方案 »

  1.   

    表单传递参数有两种方式:get 和 post 
    默认 是 get方法 传到后台会存在乱码
    需设置 属性 <form action="/Practice/RequestDemo1" method="post">
      

  2.   

    给form加个属性method="post"。
    把这段代码写到doPost里面request.setCharacterEncoding("UTF-8");
    System.out.println("用户名"+request.getParameter("username"));
    System.out.println("密码"+request.getParameter("password"));
    System.out.println("验证码"+request.getParameter("verifycode"));
      

  3.   

    如果不加method="post"属性提交默认是get提交,参数会追加到url...
    可以进入tomcat修改server.xml加上这个URIEncoding="UTF-8"。
     <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>
      

  4.   

    一般情况下你按照2-3楼的那个设置足以解决
    如果实在还是解决不了,就把需要中文传参的地方进行encodeURIcomponent后台接收的地方进行 URLEncoder.decode
      

  5.   

    get请求乱码就改server.xml post请求乱码可以直接用个拦截器不然每次写太麻烦了
      

  6.   

     response.setHeader("Content-type", "text/html;charset=UTF-8");