1 package com.dy.xidian;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5
6 import javax.servlet.ServletConfig;
7 import javax.servlet.ServletException;
8 import javax.servlet.annotation.WebInitParam;
9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 @WebServlet(initParams = {@WebInitParam(name = "startPoint", value = "1600")}, name = "LifeServlet", value = "/show")
15 public class LifeCycleServlet extends HttpServlet {
16
17 private static double startPoint = 0;
18 /**
19 * Destruction of the servlet. <br>
20 */
21 public void destroy() {
22 this.log("执行 destroy 方法...");
23 super.destroy(); // Just puts "destroy" string in log
24 // Put your code here
25 }
26
27 /**
28 * The doGet method of the servlet. <br>
29 *
30 * This method is called when a form has its tag value method equals to get.
31 *
32 * @param request
33 * the request send by the client to the server
34 * @param response
35 * the response send by the server to the client
36 * @throws ServletException
37 * if an error occurred
38 * @throws IOException
39 * if an error occurred
40 */
41 public void doGet(HttpServletRequest request, HttpServletResponse response)
42 throws ServletException, IOException {
43 this.log("执行 doGet 方法...");
44 response.setCharacterEncoding("utf-8");
45 response.setContentType("text/html");
46 PrintWriter out = response.getWriter();
47 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
48 out.println("<HTML>");
49 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
50 out.println(" <BODY>");
51 out.print("<form action='http://localhost/LifeCycleServlet/show' method='post'>");
52 out.print("请输入数字:<input type='text' name='num'/><br>");
53 out.print("<input type='submit' value='提交' />");
54 out.print("</form>");
55 out.println(" </BODY>");
56 out.println("</HTML>");
57 out.flush();
58 out.close();
59 }
60
61 /**
62 * The doPost method of the servlet. <br>
63 *
64 * This method is called when a form has its tag value method equals to
65 * post.
66 *
67 * @param request
68 * the request send by the client to the server
69 * @param response
70 * the response send by the server to the client
71 * @throws ServletException
72 * if an error occurred
73 * @throws IOException
74 * if an error occurred
75 */
76 public void doPost(HttpServletRequest request, HttpServletResponse response)
77 throws ServletException, IOException {
78 this.log("执行 doPost 方法...");
79 response.setCharacterEncoding("utf-8");
80 response.setContentType("text/html");
81 PrintWriter out = response.getWriter();
82 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
83 out.println("<HTML>");
84 out.println(" <HEAD><TITLE>个人所得税计算</TITLE></HEAD>");
85 out.println(" <BODY>");
86 out.print(request.getParameter("num"));
87 out.println(" </BODY>");
88 out.println("</HTML>");
89 out.flush();
90 out.close();
91 }
92
93 /**
94 * Initialization of the servlet. <br>
95 *
96 * @throws ServletException
97 * if an error occurs
98 */
99 public void init() throws ServletException {
100 this.log("执行 init() 方法...");
101 ServletConfig conf = this.getServletConfig();
102 startPoint = Double.parseDouble(conf.getInitParameter("startPoint"));
103 }
104
105 @Override
106 protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
107 throws ServletException, IOException {
108 this.log("执行 service() 方法....");
109 super.service(arg0, arg1);
110 }
111
112 }
访问方式
![]()
MyEclipse输出结果
![]()
![]()
生命周期详细分析