zoukankan      html  css  js  c++  java
  • 记账本开发记录——第十三天(2020.1.31)

    今天主要是学习了如何使用servletcontext来进行一个记录网站登录人数的案例。在实现这个案例之前,首先要学习相关的知识。

    ServletContext:在服务器启动的时候,为每个web应用创建一个单独的ServletContext对象,我们可以使用这个对象存储数据。

    那么我们实现这个案例的思路是如何呢?我们可以在登录的Servlet中初始一个变量count为0,然后将其存到ServletContext中,在进入页面时就读取ServletContext的内容。下面是相关代码:

    package com.itheima.login;
    
    import java.io.IOException;
    import java.sql.SQLException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    
    import com.itheima.domain.User;
    import com.itheima.utils.DataSourceUtils;
    
    public class LoginServlet extends HttpServlet {
        
        @Override
        public void init() throws ServletException {
            //在Seveltcontext域中存一个数据count
            int count = 0;
            this.getServletContext().setAttribute("count", count);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
            
            
            //username=zhangsan&password=123
            
            //1、获得用户名和密码
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            
            //2、从数据库中验证该用户名和密码是否正确
            QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
            String sql = "select * from user where username=? and password=?";
            User user = null;
            try {
                user = runner.query(sql, new BeanHandler<User>(User.class), username,password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            //3、根据返回的结果给用户不同显示信息
            if(user!=null){
                //从servletcontext中取出count进行++运算
                ServletContext context = this.getServletContext();
                Integer count = (Integer) context.getAttribute("count");
                count++;
                //用户登录成功
                response.getWriter().write(user.toString()+"---you are success login person :"+count);
                context.setAttribute("count", count);
            }else{
                //用户登录失败
                response.getWriter().write("sorry your username or password is wrong");
            }
            
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    }
  • 相关阅读:
    Java虚拟机一览表
    Java程序员的10道XML面试题
    bzoj 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课【spfa】
    bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名【bitset+Floyd传递闭包】
    bzoj 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝【dp+树状数组】
    bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】
    bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】
    bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群【最大点覆盖】
    bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
    bzoj 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛【树形dp】
  • 原文地址:https://www.cnblogs.com/wushenjiang/p/12246529.html
Copyright © 2011-2022 走看看