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>

  • 相关阅读:
    模块(module)
    编译和解释性语言和python运行方式
    管道通信初级
    Page.TryUpdateModel 方法
    运用模型绑定和web窗体显示和检索数据(Retrieving and displaying data with model binding and web forms)
    HttpResponse 类
    ASP.NET中IsPostBack详解
    3.4.1 使用过滤式扩展方法(P43-44)
    使用扩展方法(Chapter3 P39-41)
    C#对象初始或器-Chapter3 P38
  • 原文地址:https://www.cnblogs.com/scw123/p/9945488.html
Copyright © 2011-2022 走看看