十月二十四日
最近晚上都凉起来了,实在是懒,一到晚上就犯困躺在床上就不想动。但还是一直心心念着,这次就说说乱码问题,首先就是两行代码
request.setCharacterEncoding("UTF-8");
//设置响应的MIME类型,可以指定字符编码,即响应体的字符编码
response.setContentType("text/html;charset=UTF-8");
这个是关于请求与响应的设置字符编码,主要是在上传服务器器是不同的编码的不同导致,好像是新版本的服务器都已经更改了这个,建议大家用新的,否则就需要添加上面的代码。
其实还要关于HTTP协议的相关内容,建议大家能够好好学习了解这些。
package comServlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RegisterServlet extends HttpServlet { // private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTf-8"); // response.setCharacterEncoding("UTF-8"); //设置响应的MIME类型,可以指定字符编码,即响应体的字符编码 response.setContentType("text/html;charset=UTF-8"); String name = request.getParameter("name"); String possword = request.getParameter("possword"); System.out.println("name = "+ name); System.out.println("possword = "+ possword); //输出到控制台 PrintWriter out = response.getWriter(); out.println("name = "+ name); out.println("possword = " + possword); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="registerServlet" method="post"> name:<input type="text" name="name"><br> possword:<input type="password" name="possword"><br> <button>提交</button> </form> </body> </html>
这个是我写的一个简单的用户,密码还有在web返回显示输入的内容。