学习参考网站:https://www.w3cschool.cn/servlet/servlet-form-data.html
Servlet返回值乱码问题解决方法:
package gacl.Servlet.study;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloForm extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
response.setHeader("Content-type", "text/html;charset=UTF-8"); //这句话的意思,是让浏览器用utf8来解析返回的数据
response.setCharacterEncoding("UTF-8"); //这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859
PrintWriter out =response.getWriter();
String title="使用GET方法读取表单数据";
String docType=
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>名字</b>:"
+ request.getParameter("first_name") + "\n" +
" <li><b>姓氏</b>:"
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body></html>");
}
}
Servlet 生命周期过程:
1、通过调用 init () 方法进行初始化。
2、调用 service() 方法来处理客户端的请求(doGet、doPost方法)。
3、通过调用 destroy() 方法终止(结束)。
4、由 JVM 的垃圾回收器进行垃圾回收的。