今天遇到一个servlet乱码问题

新建web工程myproject

新建servlet

public class ThreeParams extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException
{
  resp.setContentType("text/html;charset=UTF-8");

  req.setCharacterEncoding("UTF-8");
  PrintWriter out=resp.getWriter();
  out.println(req.getParameter("param1"));
  out.println("<br>");
  out.println(req.getParameter("param2"));
  out.println("<br>");
  out.println(req.getParameter("param3"));
  out.println("<br>");
  System.out.println("in doGet");
}

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException
{
  System.out.println("in doPost");
 
  resp.setContentType("text/html;charset=UTF-8 ");

req.setCharacterEncoding("UTF-8");
    PrintWriter out=resp.getWriter();
  out.println(req.getParameter("param1"));
  out.println("<br>");
  out.println(req.getParameter("param2"));
  out.println("<br>");
  out.println(req.getParameter("param3"));
  out.println("<br>");
}
private static final long serialVersionUID = 1L;
}



新建jsp类 为servlet提供参数

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>
</head>
<body>
  <form action="ThreeParams" id="form1" method="post" name="form1">
   <table width="343" border="1">
    <tr>
     <td width="92">
      param1
     </td>
     <td width="94">
      <input name="param1" type="text" id="param1" />
     </td>
    </tr>

    <tr>
     <td width="92">
      param2
     </td>
     <td>
      <input name="param2" type="text" id="param2" />
     </td>
    </tr>

    <tr>
     <td>
      param3
     </td>
     <td>
      <input type="text" name="param3" id="param3" />
     </td>
    </tr>
    <tr>
     <td>
      &nbsp;
     </td>
     <td>
      <label>
       <input type="submit" value="提交" />
      </label>
     </td>
    </tr>
   </table>
  </form>
 
  <br>
  <form action="ThreeParams" id="form1" method="get" name="form1">
   <table width="343" border="1">
    <tr>
     <td width="92">
      param1
     </td>
     <td width="94">
      <input name="param1" type="text" id="param1" />
     </td>
    </tr>

    <tr>
     <td width="92">
      param2
     </td>
     <td>
      <input name="param2" type="text" id="param2" />
     </td>
    </tr>

    <tr>
     <td>
      param3
     </td>
     <td>
      <input type="text" name="param3" id="param3" />
     </td>
    </tr>
    <tr>
     <td>
      &nbsp;
     </td>
     <td>
      <label>
       <input type="submit" value="提交" />
      </label>
     </td>
    </tr>
   </table>
  </form>
</body>
</html>
接下来配置web.xml

<servlet>
  <servlet-name>threep</servlet-name>
  <servlet-class>ThreeParams</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>threep</servlet-name>
  <url-pattern>/ThreeParams</url-pattern>
</servlet-mapping>

执行 http://localhost:8080/myproject/threeparams.jsp



在表单中输入中文,发现post方式提交的表单中的中文提交后正常显示,而get方式提交的表单中中文是乱码。解决方法是修改tomcat中的server.xml,将其中的

<Connector port="8080" protocol="HTTP/1.1"
               maxThreads="150" connectionTimeout="20000"
               redirectPort="8443"  />

改为<Connector port="8080" protocol="HTTP/1.1"
               maxThreads="150" connectionTimeout="20000"
               redirectPort="8443"  URIEncoding="UTF-8"/>



就可以都正常显示中文字符了

你可能感兴趣的:(html,tomcat,jsp,Web,servlet)