zoukankan      html  css  js  c++  java
  • Java的用户登录计数功能

    package com.oracle.web;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    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 com.orecla.tools.JDBCUtils;
    
    public class loginServlet extends HttpServlet {
        public void init() throws ServletException {
            //定义计数器
            int countt = 0;
            //获取ServletContext对象
            ServletContext  context = getServletContext();    
            context.setAttribute("countt", countt);
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1、获取前台传来的值,用户名和密码
            String username = request.getParameter("username");
            String  password = request.getParameter("password");
            
            //UserDao层开始
            //2、连接数据库连接
            Connection conn = JDBCUtils.get();
            String  sql = "select count(*) from user where uname = ? and pwd = ?";
            int count = 0;
            PreparedStatement pst = null;
            ResultSet rs = null;
            try {
                pst = conn.prepareStatement(sql);
                pst.setString(1, username);
                pst.setString(2, password);
                 rs = pst.executeQuery();
                while(rs.next()){
                    count = rs.getInt(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                JDBCUtils.close(conn, pst, rs);
            }
            //UserDao层结束
            
            //判断count 值给予给前台提示
            if(count >0){
                ServletContext context = getServletContext();
                int countt = (int)context.getAttribute("countt");
                countt++; 
                response.getWriter().write("success");//前台打印
                response.getWriter().write("nishidi"+countt+"gerenfangwen");
                context.setAttribute("countt", countt);
            }else{
                response.getWriter().write("false");//前台打印
            }
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
            <form action="/WEB02/loginServlet"  method="post">
                用户名:<input type = "text" name="username"><br>
                密码:<input type="password" name="password"><br>
                <input type="submit"  value="提交">     
            </form>
    </body>
    </html>

  • 相关阅读:
    Linux中权限管理之文件特殊权限
    Linux中权限管理之ACL权限
    Linux用户管理命令
    【并发编程】实现多线程的几种方式
    “数据中台”的再思考
    软件工程六大设计原则总结,案例演示
    你必须要知道的移动端开发知识
    【搞定面试官】你还在用Executors来创建线程池?会有什么问题呢?
    EasyCode实现数据库到Swagger全自动化
    【Java实例】使用Thumbnailator生成缩略图(缩放、旋转、裁剪、水印)
  • 原文地址:https://www.cnblogs.com/scw123/p/9945488.html
Copyright © 2011-2022 走看看