使用Servlet来完成用户名和密码的登录验证以及登录人数的增加

登录页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="login">
        姓名:<input type="text" name="name"><br>
        密码:<input type="password" name="password"><br>
        <input type="submit" value="登录" >
    </form>
</body>
</html>

创建Servlet

public class LoginServlet extends HttpServlet {
    @Override //重写init方法 该方法是通过ServletContext上下文对象设置count值来完成登录人数的增加功能
    public void init() throws ServletException {
        int count = 0;  
        this.getServletContext().setAttribute("count", count); //设置域属性
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求中的属性值
        String name = request.getParameter("name"); 
        String password = request.getParameter("password");

        //从数据库中获取用户名的值 这里用了C3P0连接池
        QueryRunner queryRunner =  new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from user where name =? and password=?";
        User user_bean = null;
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), name,password);
            user_bean = user;
            ServletContext servletContext = this.getServletContext();
            if(user_bean!=null) {
                //获得域属性
                Integer count = (Integer) servletContext.getAttribute("count");
                count++; //count值自增
                servletContext.setAttribute("count", count); //设置域属性
                response.getWriter().write("good "+user_bean+"<br>"+"是第"+count+"位");
            }
            else {
                response.getWriter().write("bad");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

C3P0连接池的使用

使用前需要先导入三个jar包
这里写图片描述

之后将C3P0配置文件写入 注意要放入src目录下 这个是必须的 名称必须为c3p0-config.xml

//C3P0配置文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql:///</property>
    <property name="user">root</property>
    <property name="password">root</property>
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">20</property>
  </default-config>

  <named-config name="oracle"> 
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql:///</property>
    <property name="user">root</property>
    <property name="password">root</property>
  </named-config>


</c3p0-config>

写完配置文件后 写C3P0Utils工具类

public class C3P0Utils {
    public static ComboPooledDataSource pd = new ComboPooledDataSource();

    public static DataSource getDataSource() {
        return pd;
    }

    public static Connection getConnection() {
        try {
            return pd.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ 
        if(rs!=null){ 
            try { 
                rs.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
        if(pst!=null){ 
            try { 
                pst.close(); 
            } catch (SQLException e) { 
               e.printStackTrace(); 
            } 
        } 

        if(conn!=null){ 
            try { 
                conn.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
}

查询操作需要封装一个实体类对象 在这里用的User

public class C3P0Utils {
    public static ComboPooledDataSource pd = new ComboPooledDataSource();

    public static DataSource getDataSource() {
        return pd;
    }

    public static Connection getConnection() {
        try {
            return pd.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ 
        if(rs!=null){ 
            try { 
                rs.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
        if(pst!=null){ 
            try { 
                pst.close(); 
            } catch (SQLException e) { 
               e.printStackTrace(); 
            } 
        } 

        if(conn!=null){ 
            try { 
                conn.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值