ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器
Posted 技术让世界更精彩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器相关的知识,希望对你有一定的参考价值。
我们一般知道Servlet接口中的init()方法在tomcat启动时调用,destroy()方法在tomcat关闭时调用。那么这两个方法到底在实际开发中有什么作用呢?这就是这个随笔主要讲的内容。
思路:网站计数器,如果想在服务器关闭时,保留以前的访问量,最简单的方式是把访问数据放在数据库中,但这样会增加服务器的压力。所以ServletContext结合Servlet接口中的init()方法和destroy()方法的一起使用便可以做一个网站计数器。
把数据写在一个.txt文件中,在init()方法中把文件的数据读取出来,并添加到ServletContext中。在destroy()方法,把ServletContext中的数据取出来写入到文件中。
具体代码如下:本代码共包括的类,以及文件的位置见下图中的红色矩形框:
web.xml中的代码如下:
1 <servlet> 2 <description>This is the description of my J2EE component</description> 3 <display-name>This is the display name of my J2EE component</display-name> 4 <servlet-name>InitServlet2</servlet-name> 5 <servlet-class>com.qls.counter2.InitServlet2</servlet-class> 6 <load-on-startup>1</load-on-startup>//this is very important .you cannot ignore it . 7 </servlet> 8 <servlet> 9 <description>This is the description of my J2EE component</description> 10 <display-name>This is the display name of my J2EE component</display-name> 11 <servlet-name>Login2</servlet-name> 12 <servlet-class>com.qls.counter2.Login2</servlet-class> 13 </servlet> 14 <servlet> 15 <description>This is the description of my J2EE component</description> 16 <display-name>This is the display name of my J2EE component</display-name> 17 <servlet-name>LoginOperation2</servlet-name> 18 <servlet-class>com.qls.counter2.LoginOperation2</servlet-class> 19 </servlet> 20 <servlet> 21 <description>This is the description of my J2EE component</description> 22 <display-name>This is the display name of my J2EE component</display-name> 23 <servlet-name>Ok2</servlet-name> 24 <servlet-class>com.qls.counter2.Ok2</servlet-class> 25 </servlet>
//InitServlet2的代码如下:
1 package com.qls.counter2; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.io.PrintWriter; 10 11 import javax.servlet.ServletException; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 /** 16 * 利用tomcat启动时调用init()方法,和关闭时调用destroy()方法的特点 17 * 把上次tomcat关闭时的数目写入到文件recordnum.txt中。 18 * 启动tomcat时从该文件recordnum.txt中读取数据。 19 * 这样就避免了把这些数据不停保留在数据库中了,增加服务器的运行压力。 20 * 其实数据库的本质也是文件。 21 * @author 秦林森 22 * 23 */ 24 public class InitServlet2 extends HttpServlet { 25 26 27 public void destroy() { 28 /** 29 * 利用tomcat关闭时调用该方法的特性 30 * 把ServletContext的属性值的写入文件recordnum.txt。 31 */ 32 /** 33 * 获取文件recordnum.txt的全路径 34 */ 35 System.out.println("destory被调用了"); 36 String path = this.getServletContext().getRealPath("recordnum.txt"); 37 try { 38 BufferedWriter out=new BufferedWriter(new FileWriter(path)); 39 /** 40 * 把ServletContext中的数据写入到文件recordnum.txt中。 41 */ 42 String num = (String) this.getServletConfig().getServletContext().getAttribute("pengta"); 43 out.write(num); 44 out.close(); 45 } catch (IOException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 50 51 } 52 53 54 public void doGet(HttpServletRequest request, HttpServletResponse response) 55 throws ServletException, IOException { 56 57 response.setContentType("text/html;charset=utf-8"); 58 PrintWriter out = response.getWriter(); 59 60 } 61 62 63 public void doPost(HttpServletRequest request, HttpServletResponse response) 64 throws ServletException, IOException { 65 66 response.setContentType("text/html;charset=utf-8"); 67 PrintWriter out = response.getWriter(); 68 this.doGet(request, response); 69 } 70 71 72 public void init() throws ServletException { 73 System.out.println("init()被调用了"); 74 /** 75 * 利用启动Tomcat时调用该方法的特性。 76 */ 77 /** 78 * 获得recordnum.txt的全路径 79 */ 80 String path = this.getServletConfig().getServletContext().getRealPath("recordnum.txt"); 81 /** 82 * 从recordnum.txt中读取数据 83 */ 84 try { 85 BufferedReader in = new BufferedReader(new FileReader(path)); 86 String num = in.readLine(); 87 this.getServletConfig().getServletContext().setAttribute("pengta", num); 88 in.close();//关流。 89 } catch (FileNotFoundException e) { 90 // TODO Auto-generated catch block 91 e.printStackTrace(); 92 } catch (IOException e) { 93 // TODO Auto-generated catch block 94 e.printStackTrace(); 95 } 96 } 97 98 }
//Login2 的代码如下:
1 package com.qls.counter2; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class Login2 extends HttpServlet { 12 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 17 response.setContentType("text/html;charset=utf-8"); 18 PrintWriter out = response.getWriter(); 19 out.println("<h1>统计网站浏览的次数的登录界面</h1>"); 20 out.println("<br/>"); 21 out.println("<form action=‘/servletContext/LoginOperation2‘ method=‘post‘>"); 22 out.println("<br/>"); 23 out.println("用户名:<input type=‘text‘ name=‘username‘/>"); 24 out.println("<br/>"); 25 out.println("密码:<input type=‘password‘ name=‘password‘/>"); 26 out.println("<br/>"); 27 out.println("<input type=‘submit‘ value=‘登录‘/>"); 28 out.println("</form>"); 29 } 30 31 32 public void doPost(HttpServletRequest request, HttpServletResponse response) 33 throws ServletException, IOException { 34 35 response.setContentType("text/html;charset=utf-8"); 36 PrintWriter out = response.getWriter(); 37 this.doGet(request, response); 38 } 39 40 41 42 }
//LoginOperation2的代码如下:
1 package com.qls.counter2; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class LoginOperation2 extends HttpServlet { 12 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 17 response.setContentType("text/html;charset=utf-8"); 18 PrintWriter out = response.getWriter(); 19 /** 20 * 获取在登录界面输入的密码: 21 */ 22 String password = request.getParameter("password"); 23 //获取ServletContext中的数目 24 String ouyangfeng = (String) this.getServletConfig().getServletContext().getAttribute("pengta"); 25 /** 26 * 由于每个用户访问该网站都有一个唯一的密码和用户名,现假设该用户的密码是123,只要密码是123就能登录进去。 27 */ 28 if (password.equals("123")) { 29 30 this.getServletConfig().getServletContext().setAttribute("pengta", (Integer.parseInt(ouyangfeng)+1)+""); 31 32 response.sendRedirect("/servletContext/Ok2"); 33 }else{ 34 request.getRequestDispatcher("/Login2").forward(request, response); 35 } 36 } 37 38 39 public void doPost(HttpServletRequest request, HttpServletResponse response) 40 throws ServletException, IOException { 41 42 response.setContentType("text/html;charset=utf-8"); 43 PrintWriter out = response.getWriter(); 44 this.doGet(request, response); 45 } 46 47 }
//Ok2 的代码如下:
1 package com.qls.counter2; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class Ok2 extends HttpServlet { 12 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 17 response.setContentType("text/html;charset=utf-8"); 18 PrintWriter out = response.getWriter(); 19 out.println("访问该网站的次数是:"+this.getServletContext().getAttribute("pengta")); 20 } 21 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 26 response.setContentType("text/html;charset=utf-8"); 27 PrintWriter out = response.getWriter(); 28 this.doGet(request, response); 29 } 30 31 }
//recordnum.txt的代码如下:
recordnum.txt的内容只有一个0.如下图所示:
以上是关于ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器的主要内容,如果未能解决你的问题,请参考以下文章
ServletContextListener和ServletContext