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);
        }
    }
  • 相关阅读:
    在二进制与文本之间转换plist文件
    iOS 音频分贝的计算
    iOS 圆形水波浪效果实现
    iOS画圆、画线
    iOS IM开发准备工作(四)CocoaAsyncSocket的使用
    iOS IM开发准备工作(三)乱说Socket
    iOS IM开发准备工作(二)protobuf-objc安装及使用
    iOS IM开发准备工作(一)XML解析
    iOS IM开发blog写作计划
    西游记倒着看。。我从贴吧看来的
  • 原文地址:https://www.cnblogs.com/wushenjiang/p/12246529.html
Copyright © 2011-2022 走看看