zoukankan      html  css  js  c++  java
  • Servlet学习之计数器

    源码呈现,用户校验登录Servlet及计数完整代码:

    package com.zkj;
    //通过继承HttpServlet开发
    import javax.servlet.http.*;
    import java.io.*;
    import javax.servlet.*;

    public class Check extends HttpServlet{

      public void init() throws ServletException{
        try{
          System.out.println("Init被调用");
          //添加网页访问次数的功能
          //获取当前次数
          FileReader fr = null;
          BufferedReader bfr = null;
          String sCount;
          try
          {
            fr = new FileReader("D:\Data.txt");
            bfr = new BufferedReader(fr);
            sCount = bfr.readLine();

            this.getServletContext().setAttribute("loginTimes", sCount);
          }catch(Exception e){
          }finally{
            if (bfr != null)
              bfr.close();
          }

        }catch(Exception e){
        }
      }

      public void destroy(){
        try{
          //再将新的次数写回文件
          FileWriter fw = null;
          BufferedWriter bfw = null;
          try{
            fw = new FileWriter("D:\Data.txt");
            bfw = new BufferedWriter(fw);
            bfw.write("" + this.getServletContext().getAttribute("loginTimes"));
          }catch(Exception e){
          }
          finally{
            if (bfw != null)
              bfw.close();
          }

          System.out.println("Destroy被调用");
        }catch(Exception e){
        }  
      }

      //处理get请求
      public void doGet(HttpServletRequest req, HttpServletResponse res){
        System.out.println("service it");
        try{

          //接收用户名和密码
          String u=req.getParameter("username");
          String p=req.getParameter("passwd");
          //连接数据库
          UserBeanManager ubm = new UserBeanManager();
          boolean bHas = ubm.checkUserValid(u, p);

          //验证
          //u.equals("zkj") && p.equals("123")
          if (bHas)
          {
            //合法用户进入

            String chk = req.getParameter("keep");
            if (chk != null){
              //将用户名和密码保存在cookie
              Cookie ck = new Cookie("myname", u);
              Cookie ps = new Cookie("mypass", p);
              //设置生命长
              ck.setMaxAge(14*24*3600);
              ps.setMaxAge(14*24*3600);
              //回写到客户端
              res.addCookie(ck);
              res.addCookie(ps);
            }

            //计数增加写到ServletContext
            String sCount = this.getServletContext().getAttribute("loginTimes").toString();
            int nCount = Integer.parseInt(sCount);
            nCount ++;
            this.getServletContext().setAttribute("loginTimes", nCount + "");

            //传送name到welcome
            res.sendRedirect("welcome?uname=" + u);
          }else{
            res.sendRedirect("login");
          }

        }catch(Exception e){
          e.printStackTrace();
        }
      }

      //处理post请求 一般是共用,用一个业务逻辑
      public void doPost(HttpServletRequest req, HttpServletResponse res){
        this.doGet(req, res);
      }

    }

    为了避免多次读写文件,需要把读原始计数在init中,保存计数值写到destroy

  • 相关阅读:
    让UILabel的文字顶部对齐
    常用的iOS开发或者优化的小工具
    AppStoreID--安装URL--应用更新URL--应用评分URL
    iOS 下载功能:断点下载(暂停和开始)(NSURLConnectionDataDelegate方法)
    iOS QLPreviewController(Quick Look)快速浏览jpg,PDF,world等
    如何不让UITableView滚动
    解析字典包含关键字比如ID,description等,MJExtension 框架 不能直接设置变量与其同名。
    今天犯了个小错误:_dataArray.count>1 和_dataArray.count>0搞混淆了
    获取当前的日期和时间-数码
    C/C++中的段错误(Segmentation fault)[转]
  • 原文地址:https://www.cnblogs.com/jiqiwoniu/p/4412336.html
Copyright © 2011-2022 走看看