从jsp获取的数据是汉字的时候,会有在servlet中出现乱码的情况,本人实际遇到的问题
已下是解决的办法:
解决方案1:
直接把get方法改为post方法实现;
解决方案2:(直接以代码为例)
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
System.out.println(new String(request.getParameter("type").getBytes("ISO8859_1"), "utf-8")); //这是重点,主要是使用这个方法
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<p>"+new String(request.getParameter("type").getBytes("ISO8859_1"), "utf-8")+"</p>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}