Java后台开发中乱码的转换

public class RequestServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String admin = req.getParameter("admin");
		admin = new String(admin.getBytes("ISO8859-1"), "UTF-8");//将ISO8859-1编码格式转换成UTF-8
		String password = req.getParameter("password");
		System.out.println(admin + "==" + password);
		resp.setHeader("Content-type", "text/html;charset=UTF-8");//将UTF-8编码格式转换成ISO8859-1
		resp.getWriter().write(admin + "==" + password);
	}

	
}

 

以上的处理的是Get请求,以下是Post请求

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");//将ISO8859-1编码格式转换成UTF-8
		String admin = req.getParameter("admin");
		String password = req.getParameter("password");
		System.out.println(admin + "==" + password);
		resp.setHeader("Content-type", "text/html;charset=UTF-8");//将UTF-8编码格式转换成ISO8859-1
		resp.getWriter().write(admin + "==" + password);
	}

 

你可能感兴趣的:(Java,EE)