最近在学servlet,遇到了一些问题都将记录在这里
对于表单中的中文请求乱码问题,无非就是在doPost()方法里加上
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
然而,我这么做了之后却依旧乱码,eclipse一直输出‘???’,最后解决的方法是在发送表单的方法中加了一句“response.setCharacterEncoding("utf-8");,问题解决”,对servlet的机理还不大熟,不明白这是什么原因,就先记录着
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String login="";
HttpSession session = request.getSession(false);
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println(
"<form method='POST' action='"
+ response.encodeURL(request.getContextPath()+"/Student")
+ "'>");
out.println(
"login: <input type='text' name='login' value='" + login + "'>");
out.println(
"password: <input type='password' name='password' value=''>");
out.println("<input type='submit' name='Submit' value='Submit'>");
out.println("</form></body></html>");
}