利用ServletContext实现网页计数器

(1)思路分析:

使用ServletContext 和 记事本两个东西,将登陆的次数写在txt文件中,首先是在在登陆的条件下 利用Servlet的init()函数初始化打开txt计数器文件并读取然后在用户登录后的过程中访问网站不会再重复技术只计数一次,直到注销之后destroy()然后计数器文件加1;

(2)登陆界面

[java]  view plain  copy
  1. package com.tsinghua;  
  2.   
  3. import javax.servlet.http.*;  
  4.   
  5. import java.io.*;  
  6.   
  7.   
  8. public class Login extends HttpServlet {  
  9.       
  10.     public void doGet(HttpServletRequest req,HttpServletResponse res){  
  11.           
  12.         //业务逻辑   
  13.           
  14.         try {  
  15.                   
  16.             //中文乱码  
  17.             res.setContentType("text/html;charset=gbk");  
  18.               
  19.             PrintWriter pw=res.getWriter();  
  20.               
  21.             //返回登录界面  
  22.             pw.println("<html>");  
  23.             pw.println("<body bgcolor=#CED3FF>");  
  24.               
  25.             pw.println("<img src=imgs/1.GIF><hr><center>");  
  26.             //得到error信息  
  27.             String info=req.getParameter("info");  
  28.               
  29.             if(info!=null){  
  30.                 pw.println("<h1>你的用户名或是密码错误!</h1><br>");  
  31.             }  
  32.             pw.println("<h1>登录界面</h1>");  
  33.             pw.println("<form action=loginCl method=post>");  
  34.             pw.println("用户名:<input type=text name=usrename><br>");  
  35.             pw.println("密码:<input type=password name=passwd><br>");  
  36.             pw.println("<input type=checkbox name=keep value=2>两周内不再重新登录<br>");  
  37.             pw.println("<input type=submit value=login><br>");  
  38.             pw.println("</form>");  
  39.             pw.println("</center><hr><img src=imgs/mylogo.gif>");  
  40.             pw.println("</body>");  
  41.             pw.println("</html>");  
  42.               
  43.         }  
  44.         catch (Exception ex) {  
  45.               
  46.             ex.printStackTrace();  
  47.         }  
  48.     }  
  49.       
  50.     public void doPost(HttpServletRequest req,HttpServletResponse res){  
  51.           
  52.         this.doGet(req,res);  
  53.           
  54.     }  
  55. }  


(2)处理登陆页面的请求

[java]  view plain  copy
  1. package com.tsinghua;  
  2.   
  3. import javax.servlet.http.*;  
  4.   
  5. import java.io.*;  
  6. import java.sql.*;  
  7.   
  8. public class LoginCl extends HttpServlet {  
  9.       
  10.   
  11.     //重写init函数    
  12.     public void init(){  
  13.                   
  14.         try {  
  15.                   
  16.             //只会被调用一次  
  17.               
  18.             //创建一个FileReader  
  19.             FileReader  f=new FileReader("f:\\myCounter.txt");  
  20.                   
  21.             BufferedReader br=new BufferedReader(f);  
  22.                   
  23.            
  24.             //读出一行数据      
  25.             String numVal=br.readLine();  
  26.                
  27.              //一定要关闭文件流       
  28.             br.close();  
  29.               
  30.               
  31.             //将times值放入到servletcontext  
  32.             this.getServletContext().setAttribute("visitTimes",numVal);  
  33.                   
  34.             System.out.println ("inti 被调用");  
  35.         }  
  36.         catch (Exception ex) {  
  37.           
  38.             ex.printStackTrace();  
  39.         }  
  40.           
  41.     }  
  42.       
  43.     //重写destroy函数  
  44.   
  45.     public void destroy(){  
  46.           
  47.         try {  
  48.               
  49.               
  50.             //在将新的次数写会去  
  51.             FileWriter  fw=new FileWriter("f:\\myCounter.txt");  
  52.           
  53.             <span style="white-space:pre">    </span>BufferedWriter bw=new BufferedWriter(fw);  
  54.           
  55.             <span style="white-space:pre">    </span>//在文件中写入数据   
  56.             <span style="white-space:pre">    </span>bw.write(this.getServletContext().getAttribute("visitTimes").toString());  
  57.           
  58.             <span style="white-space:pre">    </span>//关闭文件流  
  59.             <span style="white-space:pre">    </span>bw.close();  
  60.               
  61.               
  62.             System.out.println ("destroy 被调用");  
  63.         }  
  64.         catch (Exception ex) {  
  65.               
  66.             ex.printStackTrace();  
  67.           
  68.         }  
  69.     }  
  70.     //处理get请求  
  71.     //req: 用于获得客户端(浏览器)的信息  
  72.     //res: 用于向客户端(浏览器)返回信息  
  73.     public void doGet(HttpServletRequest req,HttpServletResponse res){  
  74.           
  75.         //业务逻辑   
  76.         Connection ct=null;  
  77.         Statement sm=null;  
  78.         ResultSet rs=null;  
  79.         try {  
  80.               
  81.               
  82.             //接受用户名和密码  
  83.             String u=req.getParameter("usrename");  
  84.             String p=req.getParameter("passwd");  
  85.               
  86.             //调用UserBeanCl,1.创建一个对象  
  87.             UserBeanCl ubc=new UserBeanCl();  
  88.               
  89.   
  90.             //2.使用UserBeanCl的方法  
  91.             if(ubc.checkUser(u,p)){  
  92.                   
  93.                       
  94.                     //合法用户  
  95.                       
  96.                     String keep=req.getParameter("keep");  
  97.                       
  98.                       
  99.                     if(keep!=null){  
  100.                     //将用户名和密码保存在客户端(cookie)  
  101.                     //创建  
  102.                     Cookie name=new Cookie("myname",u);  
  103.                     Cookie pass=new Cookie("mypasswd",p);  
  104.                       
  105.                     //设置时间   
  106.                     name.setMaxAge(14*24*3600);  
  107.                     pass.setMaxAge(14*24*3600);  
  108.                       
  109.                     //回写到客户端  
  110.                       
  111.                     res.addCookie(name);  
  112.                     res.addCookie(pass);  
  113.                       
  114.                 }  
  115.                       
  116.                       
  117.                       
  118.                       
  119.                 //将用户名和密码放入到session中  
  120.                 HttpSession hs=req.getSession(true);  
  121.                   
  122.                 hs.setMaxInactiveInterval(30);  
  123.                   
  124.                 hs.setAttribute("uname",u);  
  125.                   
  126.                 //将serlvetContext中的 visitTime所对应的值++  
  127.                 String times=this.getServletContext().getAttribute("visitTimes").toString();  
  128.                   
  129.                 //对times++再重新放回servlet  
  130.                 this.getServletContext().setAttribute("visitTimes",(Integer.parseInt(times)+1)+"");  
  131.                   
  132.               
  133.                 //跳转到wel  
  134.                 res.sendRedirect("Main");  
  135.                   
  136.             }else{  
  137.                   
  138.                 //不合法  
  139.                   
  140.                 //跳转  
  141.                 res.sendRedirect("login");//写你要到的servlet的那个url  
  142.             }  
  143.               
  144.         }  
  145.         catch (Exception ex) {  
  146.               
  147.             ex.printStackTrace();  
  148.         }finally{  
  149.               
  150.             try {  
  151.                   
  152.                 if(rs!=null){  
  153.                   
  154.                 rs.close();  
  155.                 }  
  156.                 if(sm!=null){  
  157.                       
  158.                     sm.close();  
  159.                 }  
  160.                 if(ct!=null){  
  161.                       
  162.                     ct.close();  
  163.                 }  
  164.             }  
  165.             catch (Exception ex) {  
  166.                   
  167.                 ex.printStackTrace();  
  168.             }  
  169.               
  170.               
  171.         }  
  172.     }  
  173.       
  174.     //处理post请求  
  175.     //req: 用于获得客户端(浏览器)的信息  
  176.     //res: 用于向客户端(浏览器)返回信息  
  177.     public void doPost(HttpServletRequest req,HttpServletResponse res){  
  178.           
  179.         this.doGet(req,res);  
  180.           
  181.     }  
  182. }  


(3)登陆成功后提交给wel页面

[java]  view plain  copy
  1. package com.tsinghua;  
  2.   
  3. import javax.servlet.http.*;  
  4.   
  5. import java.io.*;  
  6. import java.sql.*;  
  7. import java.util.*;  
  8.   
  9. public class Wel extends HttpServlet {  
  10.       
  11.   
  12.     //处理get请求  
  13.     //req: 用于获得客户端(浏览器)的信息  
  14.     //res: 用于向客户端(浏览器)返回信息  
  15.     public void doGet(HttpServletRequest req,HttpServletResponse res){  
  16.           
  17.         //业务逻辑   
  18.         Connection ct=null;  
  19.         PreparedStatement ps=null;  
  20.         ResultSet rs=null;  
  21.           
  22.         //业务逻辑   
  23.           
  24.         try {  
  25.           
  26.               
  27.             //从session中得到用户名  
  28.             HttpSession hs=req.getSession(true);  
  29.             String myName=(String)hs.getAttribute("uname");  
  30.             String name="";  
  31.             String passwd="";  
  32.             if(myName==null){  
  33.                   
  34.                 //如果session中没有用户信息,再看看有没有cookie信息  
  35.                 //从客户端得到所有cookie信息  
  36.                 Cookie [] allCookies=req.getCookies();  
  37.           
  38.                 int i=0;  
  39.                 //如果allCookies不为空...  
  40.                 if(allCookies!=null){  
  41.               
  42.                     //从中取出cookie  
  43.                     for(i=0;i<allCookies.length;i++){  
  44.                   
  45.                         //依次取出  
  46.                         Cookie temp=allCookies[i];  
  47.                   
  48.                         if(temp.getName().equals("myname")){  
  49.                           
  50.                             //得到cookie的值  
  51.                             name=temp.getValue();  
  52.                         }  
  53.                         else if(temp.getName().equals("mypasswd")){  
  54.                             passwd=temp.getValue();  
  55.                         }  
  56.                           
  57.                     }  
  58.                       
  59.                     System.out.println ("myname="+name+" pas="+passwd);  
  60.                     if(!name.equals("")&&!passwd.equals("")){  
  61.                       
  62.                         //到logincl去验证  
  63.                         res.sendRedirect("loginCl?usrename="+name  
  64.                         +"&passwd="+passwd);  
  65.                         return;  
  66.                     }  
  67.                           
  68.                 }  
  69.                   
  70.                 //返回登录界面  
  71.                 res.sendRedirect("login?info=error1");  
  72.                 return ;  
  73.             }  
  74.               
  75.             //解决中文乱码  
  76.             res.setCharacterEncoding("gbk");  
  77.               
  78.             PrintWriter pw=res.getWriter();  
  79.             pw.println("<body bgcolor=#CED3FF>");  
  80.                       
  81.               
  82.             //添加网页访问次数的功能             
  83.               
  84.             pw.println("该网页被访问了"+this.getServletContext().getAttribute("visitTimes").toString()+"次<br>");           
  85.   
  86.             pw.println("</body>");  
  87.           
  88.         }  
  89.         catch (Exception ex) {  
  90.               
  91.             ex.printStackTrace();  
  92.         }  
  93.     }  
  94.       
  95.     public void doPost(HttpServletRequest req,HttpServletResponse res){  
  96.           
  97.         this.doGet(req,res);  
  98.           
  99.     }  
  100. }  

猜你喜欢

转载自blog.csdn.net/try_and_do/article/details/79849827