Servlet用户登录功能实现

Posted jepson6669

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet用户登录功能实现相关的知识,希望对你有一定的参考价值。

需求:完成用户登录页面校验

第一步:创建一个用户登录的html页面

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

第二步:导入所需要的jar包

导入jar之后记得add to bulid path.

第三步:创建c3p0连接池的配置文件,文件名为c3p0-config.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///web13</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="myconfig">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///web13</property>
        <property name="user">root</property>
        <property name="password">root</property>
    </named-config>


</c3p0-config>

 

文件放到src目录下

 

第四步:创建c3p0工具类

package p1.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

    public static DataSource getDataSource() {
        return dataSource;
    }

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

第五步:创建User类

package p3.domain;

public class User {
    private int uid;
    private String username;
    private String upassword;
    private String email;
    public User(){}
    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUpassword() {
        return upassword;
    }
    public void setUpassword(String upassword) {
        this.upassword = upassword;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "User [uid=" + uid + ", username=" + username + ", upassword=" + upassword + ", email=" + email + "]";
    }
    
}

第六步:编写LoginServlet类

package p2.login_servlet;

import java.io.IOException;
import java.sql.SQLException;

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 p1.utils.C3P0Utils;
import p3.domain.User;

public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1 获取用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 2查询数据库中是否有相应的用户名和密码
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from user where username=? and upassword=?";
        Object[] params = {username,password};
        User user = null;
        try {
            user = qr.query(sql, new BeanHandler<User>(User.class), params);
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        // 3 根据返回结果给用户不同的显示信息
        if(user!=null){
            response.getWriter().write(user.toString());
            response.getWriter().write(user.getUsername()+":登录成功");
        }else{
            response.getWriter().write("sorry the username or password is wrong");
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

第七部:发布的时候如果出现找不到网页404的情况,有时候重新启动tomcat就可以了。

 

以上是关于Servlet用户登录功能实现的主要内容,如果未能解决你的问题,请参考以下文章

Servlet实现用户的登录功能

Servlet用户登录功能实现

jsp+servlet+mysql 实现简单的银行登录转账功能

简单使用JDBC和Servlet实现用户注册和登录功能

使用servlet实现用户登录

java web实现简单的用户登录需要哪些技术