第一种情况:post方法
<h1>我的表单</h1> <form action="/encoding/GetInfoServlet" method="post"> 名字:<input type="text" name="username" > 密码:<input type="password" name="passord"> <input type="submit" value="确认"> </form> //接收http请求 response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8");
//这里加一个 request.setCharacterEncoding("utf-8");可解决
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
out.println("name:="+username);
第二种情况:get方法和超链接一样
<form action="/encoding/GetInfoServlet" method="get"> String username = new String (request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
<a href="/encoding/GetInfoServlet?username=你还哦">测试</a>
第三种情况:sendRedirect()
//好像也是转一下就ok了 不知道为什么其他人会乱码 String filename = new String(request.getParameter("filename").getBytes("iso-8859-1"),"utf-8");