解决tomcat的和浏览器乱码的问题

package zhangeweb.atzhang.Httpservlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**

  • Servlet implementation class HhttpServlet
    */
    public class HhttpServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**

    • @see HttpServlet#HttpServlet()
      */
      public HhttpServlet() {
      super();
      // TODO Auto-generated constructor stub
      }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      /
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      /

      • 在服务器中的乱码问题
        */
        //解决冲浏览器中发送请求到服务器的时候由于编码格式的不同造成乱码
        //–解决方式:修改附服务器的配置文件(在tomcat的配置文件中servlet.xml中添加一个属性)URIEncoding=“UTF-8”(get请求)
        //–解决方式:(post请求)//在解析是修改解析的编码格式
        request.setCharacterEncoding(“Utf-8”);

      /*

      • 在浏览器解析的时候发生的乱码问题的解决方式(post和get请求在这个时候不分成两种情况)
      • (解决方法:)
        */
        response.setContentType(“text/html; charset=utf-8”);
        String name=request.getParameter(“username”);
        System.out.println(“用户的姓名是:”+name);
        String password=request.getParameter(“password”);
        System.out.println(“用户的密码是:”+password);
        response.getWriter().write(“你好呀”);//浏览器解析的时候会发生乱码,的情况
        response.getWriter().append("Served at: ").append(request.getContextPath());
        }

    /**

    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doGet(request, response);
      }

}

你可能感兴趣的:(javaweb)