Servlet会在服务器启动或第一次请求该Servlet的时候开始生命周期,在服务器停止的时候结束生命周期.
无论请求多少次Servlet,最多只有一个Servlet实例.多个客户端并发请求Servlet时,服务器会启动多个线程分别执行该Servlet的service()方法.
package com.helloxr.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LifeCycleServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 9898777879L; public static double startPoint = 0; /** * Constructor of the object. */ public LifeCycleServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { this.log("执行 destroy方法 ..."); startPoint = 0; } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.log("执行 doGet() 方法 ..."); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>个人所得税计算</TITLE></HEAD>"); out.println("<link rel=‘stylesheet‘ type=‘text/css‘ href=‘../css/style.css‘>"); out.println(" <BODY>"); out.println("<div align=‘center‘><br/><fieldset style=‘width:90%‘><legend>个税计算器</legend><br/>"); out.println("<form method=‘post‘>"); out.println("<div style=‘line‘>"); out.println(" <div class=‘leftDiv‘>您的工资为</div><div align=‘left‘ class=‘rightDiv‘><input type=‘text‘ name=‘income‘>单位:元<div>"); out.println("</div><br/>"); out.println("<div style=‘line‘"); out.println(" <div class=‘leftDiv‘></div><div align=‘left‘ class=‘rightDiv‘><input type=‘submit‘ value=‘ 计算个税 ‘ class=button></div>"); out.println("</div>"); out.println("</form>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.log("执行 doPost() 方法 ..."); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>个人所得税计算</TITLE></HEAD>"); out.println("<link rel=‘stylesheet‘ type=‘text/css‘ href=‘../css/style.css‘>"); out.println(" <BODY>"); out.println("<div align=‘center‘><br/>"); out.println("<fieldset style=‘width:90%‘><legend>个税计算器</legend><br/>"); try{ double income = new Double(request.getParameter("income")); double charge = income - startPoint; double tax = 0; if(charge <= 0) {tax = 0;} if(charge > 0 && charge <= 500) {tax = charge * 0.05;} if(charge > 500 && charge <= 2000) {tax = charge * 0.1 - 25;} if(charge > 2000 && charge <= 5000) {tax = charge * 0.15 - 125;} if(charge > 5000 && charge <= 20000) {tax = charge * 0.2 - 375;} if(charge > 20000 && charge <= 40000) {tax = charge * 0.25 - 1375;} if(charge > 40000 && charge <= 60000) {tax = charge * 0.30 - 3375;} if(charge > 60000 && charge <= 80000) {tax = charge * 0.35 - 6375;} if(charge > 80000 && charge <= 100000) {tax = charge * 0.4 - 10375;} if(charge > 100000) {tax = charge * 0.45 - 15375;} out.println("<div style=‘line‘>"); out.println(" <div class=‘leftDiv‘>您的工资为</div><div class=‘rightDiv‘>" + income + " 元</div>"); out.println("</div>"); out.println("<div style=‘line‘>"); out.println(" <div class=‘leftDiv‘>您应纳税</div><div class=‘rightDiv‘>" + tax + "元</div> "); out.println("</div><br/>"); out.println("<input type=‘button‘ onclick=‘history.go(-1);‘ value=‘纳税光荣 逃税可耻 返回‘ class=button>"); }catch(Exception e){ out.println("请输入数值类型数据.<input type=‘button‘ onclick=‘history.go(-1);‘ value=‘返回‘ class=button>"); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ @Override public void init() throws ServletException { this.log("执行 init() 方法 ..."); ServletConfig config = this.getServletConfig(); startPoint = Double.parseDouble(config.getInitParameter("startPoint")); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ this.log("执行 service() 方法 ..."); super.service(request, response); } }
web.xml的配置如下:
<servlet> <servlet-name>LifeCycleServlet</servlet-name> <servlet-class>com.helloxr.servlet.LifeCycleServlet</servlet-class> <init-param> <param-name>startPoint</param-name> <param-value>1600</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>LifeCycleServlet</servlet-name> <url-pattern>/servlet/LifeCycleServlet</url-pattern> </servlet-mapping>
Tomcat控制台的部分输出:
一月 07, 2017 10:28:35 下午 org.apache.catalina.core.ApplicationContext log 信息: LifeCycleServlet: 执行 init() 方法 ... 一月 07, 2017 10:28:35 下午 org.apache.catalina.core.ApplicationContext log 信息: LifeCycleServlet: 执行 service() 方法 ... 一月 07, 2017 10:28:35 下午 org.apache.catalina.core.ApplicationContext log 信息: LifeCycleServlet: 执行 doGet() 方法 ... 一月 07, 2017 10:29:03 下午 org.apache.catalina.core.ApplicationContext log 信息: LifeCycleServlet: 执行 service() 方法 ... 一月 07, 2017 10:29:03 下午 org.apache.catalina.core.ApplicationContext log 信息: LifeCycleServlet: 执行 doPost() 方法 ... 一月 07, 2017 10:29:22 下午 org.apache.catalina.core.StandardServer await 信息: A valid shutdown command was received via the shutdown port. Stopping the Server instance. 一月 07, 2017 10:29:22 下午 org.apache.coyote.AbstractProtocol pause 信息: Pausing ProtocolHandler ["http-apr-8080"] 一月 07, 2017 10:29:22 下午 org.apache.coyote.AbstractProtocol pause 信息: Pausing ProtocolHandler ["ajp-apr-8009"] 一月 07, 2017 10:29:22 下午 org.apache.catalina.core.StandardService stopInternal 信息: Stopping service Catalina 一月 07, 2017 10:29:22 下午 org.apache.catalina.core.ApplicationContext log 信息: SessionListener: contextDestroyed() 一月 07, 2017 10:29:22 下午 org.apache.catalina.core.ApplicationContext log 信息: ContextListener: contextDestroyed() 一月 07, 2017 10:29:22 下午 org.apache.catalina.core.ApplicationContext log 信息: LifeCycleServlet: 执行 destroy方法 ... 一月 07, 2017 10:29:22 下午 org.apache.coyote.AbstractProtocol stop 信息: Stopping ProtocolHandler ["http-apr-8080"] 一月 07, 2017 10:29:23 下午 org.apache.coyote.AbstractProtocol stop 信息: Stopping ProtocolHandler ["ajp-apr-8009"] 一月 07, 2017 10:29:23 下午 org.apache.coyote.AbstractProtocol destroy 信息: Destroying ProtocolHandler ["http-apr-8080"] 一月 07, 2017 10:29:23 下午 org.apache.coyote.AbstractProtocol destroy 信息: Destroying ProtocolHandler ["ajp-apr-8009"]
LifeCycleServlet运行效果:
原文:http://www.cnblogs.com/datapool/p/6260569.html