javaweb实现简单登陆功能

javaweb实现登陆

用户登陆首页

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/6/4
  Time: 19:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form method="POST" action="/login">
        <div class="info">${error}</div>
        <div class="form-group">
            <label for="userName">userName</label>

            <input id="userName" type="text" class="form-control" name="userName" value="" required autofocus>
        </div>

        <div class="form-group">
            <label for="password">Password</label>

            <input id="password" type="password" class="form-control" name="password" required data-eye>
        </div>

        <div class="form-group no-margin">
            <button type="submit" class="btn btn-primary btn-block" >
                Login
            </button>
        </div>
        <%--<div class="margin-top20 text-center">
            Don't have an account? <a href="register.html">Create One</a>
        </div>--%>
    </form>

</body>
</html>

用户类

package com.mrh.pojo**;

** /*
* @ClassName User
* @Description* TODO
* ** @Author m
* @Version 1.0
*/
* public class User {
private Integer id
*;
** private String userName**;
** private String password**;

** @Override
public String toString() {
return “User{” +
“id=” + id +
“, userName=’” + userName + ‘’’ +
“, password=’” + password + ‘’’ +
‘}’**;
** }

public Integer getId() {
return id**;
** }

public void setId(Integer id) {
this.id = id**;
** }

public void setUserName(String userName) {
this.userName = userName**;
** }

public void setPassword(String password) {
this.password = password**;
** }

public String getUserName() {
return userName**;
** }

public String getPassword() {
return password**;
** }
}

用户基础类:

写了一个通过用户名返回查找到的用户的函数

package com.mrh.dao.user**;

** import com.mrh.dao.BaseDao**;
** import com.mrh.pojo.User**;

** import java.sql.ResultSet**;
** import java.sql.SQLException**;

** /*
* @ClassName UserDaoImp1
* @Description* TODO
* ** @Author m
* @Version 1.0
*/
* public class UserDaoImp1 implements UserDao {
/

*
* @param* userName
* ** @return
通过用户名查找用户*,*返回第一个查找到的用户
* ** @throws
*SQLException
* **/
* public User getUser(String userName) throws SQLException {

 String sql = "select * from user where username = ?"**;

** Object[] parm = {userName}**;

** BaseDao.connection();
** ResultSet res = BaseDao.query(sql
,parm);

** User user = null**;

** while (res.next()){
user = new User();
** user.setId((Integer) res.getObject(“id”))
;
** user.setUserName((String) res.getObject(“username”));
** user.setPassword((String) res.getObject(“password”))
;
** }
BaseDao.close();
** return user
;

** }

}

用户服务类

通过调用用户基础类与得到的用户密码比较,相同则返回该用户

package com.mrh.service**;

** import com.mrh.dao.user.UserDao**;
** import com.mrh.dao.user.UserDaoImp1**;
** import com.mrh.pojo.User**;

** import java.sql.SQLException**;

** /*
* @ClassName UserLogin
* @Description* *通过调用用户基础类与得到的用户密码比较,相同则返回该用户
* ** @Author m
* @Version 1.0
*/
* public class UserService {

UserDao userDao = new UserDaoImp1()**;

** public User userLogin(String userName**,** String password) throws SQLException {
User user = userDao.getUser(userName);
** if(user != null && user.getPassword().equals(password)){
return user
;
** }else {
return null**;
** }
}

}

数据库连接基础类:

package com.mrh.dao;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @ClassName BaseDao
 * @Description TODO
 * @Author m
 * @Version 1.0
 */
public class BaseDao {

    private static String  driver;
    private static String  url;
    private static String  username;
    private static String  password;

    //静态代码块,类加载的时候就初始化了
    static {
        Properties properties = new Properties();
        //通过类加载器读取对应资源
        InputStream inputStream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");

        try {
            properties.load(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }

        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");

    }

    private static Connection con = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet res = null;

    public static Connection getCon() {
        return con;
    }

    public static PreparedStatement getPreparedStatement() {
        return preparedStatement;
    }

    public static ResultSet getRes() {
        return res;
    }

    //数据库连接
    public static void connection(){
        try{
            Class.forName(driver);
            con = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //关闭数据库连接
    public static void close(){
        if(res != null){
            try{
                res.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
        if(preparedStatement != null){
            try{
                preparedStatement.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
        if(con != null){
            try{
                con.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
    }

    //查询公共类
    public static ResultSet query(String sql, Object[] parm) throws SQLException {
        preparedStatement = con.prepareStatement(sql);
        for (int i = 0; i < parm.length; i++) {
            preparedStatement.setObject(i+1,parm[i]);
        }
        res = preparedStatement.executeQuery();
        return res;
    }

    //update公共类
    public static int update(String sql, Object[] parm) throws SQLException {
        preparedStatement = con.prepareStatement(sql);
        for (int i = 0; i < parm.length; i++) {
            preparedStatement.setObject(i+1,parm[i]);
        }
        return preparedStatement.executeUpdate();
    }

}

控制层loginServlet:

package com.mrh.servlet**;

** import com.mrh.pojo.User**;
** import com.mrh.service.UserService**;

** import javax.servlet.ServletException**;
** import javax.servlet.http.HttpServlet**;
** import javax.servlet.http.HttpServletRequest**;
** import javax.servlet.http.HttpServletResponse**;
** import java.io.IOException**;
** import java.sql.SQLException**;

** /*
* @ClassName loginServlet
* @Description* TODO
* ** @Author m
* @Version 1.0
*/
* public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req
*,** HttpServletResponse resp) throws ServletException**,** IOException {

​ //获取用户名和密码
​ String userName = req.getParameter(“userName”);
** String password = req.getParameter(“password”)
;

** UserService userService = new UserService();
** User user = null
;
** try {
user = userService.userLogin(userName**,** password);
** } catch (SQLException throwables) {
throwables.printStackTrace()
;
** }

​ if(user == null){
​ //登陆失败,返回错误信息
​ req.setAttribute(“error”,“用户名或密码错误”);
** //转发到登陆界面,所以url不会变
​ req.getRequestDispatcher(“index.jsp”).forward(req
,resp);
** }else {
​ resp.getWriter().write(“登陆成功”)**;
** }

}

@Override
protected void doPost(HttpServletRequest req**,** HttpServletResponse resp) throws ServletException**,** IOException {
doGet(req**,** resp)**;
** }

}

测试结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值