1.写在index.jsp中
<a href="servlet/ChuanHanZiServlet?name=哈哈">哈哈</a>
2.写在servlet中
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
name=new String(name.getBytes("ISO-8859-1"),"utf-8");
System.out.println(name);
response.setContentType("text/html;charset=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.print("name="+name);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
3.经过——String name=request.getParameter("name"); name=new String(name.getBytes("ISO-8859-1"),"utf-8");这两条语句就可以避免在控制台和页面输出时乱码