013、(案例)登录案例,联合mysql

注意我们在本利中使用BeanUtils类的静态方法populate()将用户输入的登录信息转成到本地的user类中,我们需要注意在login.html几个<input>的name属性需要和user类中的属性的名字要相同。

将用户输入的信息

1、网页显示最原始的登录页面。

2、用户填写了数据后登录后,会连接到服务器。

3、服务器将浏览器用户传来的信息组合到mysql查询语句中发送到mysql进行查询,如果验证了用户名和密码都正确,则显示登录成功,如果验证失败则显示登录失败。

整个项目信息如下:

1、配置文件

druid.properties(放在src目录下)

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///demosql
username=root
password=tt1314521
initialSize=5
maxActive=10
maxWait=3000

二、登录界面文件

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<body>
    <form action="loginS" name="login_in" method="post">

        <label for="username">用户名</label><input type="text" name="username" id="username" placeholder="请输入用户名" >
        <br>
        <label for="password">密码</label><input type="password" name="password" id="password" placeholder="请输入密码">
        <br>
        <input type="submit" value="提交">
    </form>

</body>
</html>

3、servlet类文件

(1)、LoginServlet.java,登录

import cn.lw.dao.Daouser;
import cn.lw.domain.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/loginS")
public class LoginServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//
//        User usr_login = new User();
//        usr_login.setUser(username);
//        usr_login.setPassword(password);

//        采用BeanUtils工具类将浏览器发送的参数放到User类中去
        Map<String, String[]> parameterMap = req.getParameterMap();
        User user_login = new User();
        try {
            BeanUtils.populate(user_login,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        Daouser daouser = new Daouser();
        User user = daouser.loginUser(user_login);
        if (user == null){
            req.getRequestDispatcher("failServlet").forward(req,resp);
        }else{
            req.setAttribute("user",user);
            req.getRequestDispatcher("successServlet").forward(req,resp);

        }


    }

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

(2)、failServlet.java,验证失败跳转

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/failServlet")
public class failServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("type/html;charset=utf-8");
        response.getWriter().write("用户名或密码错误!");
    }
}

(2)验证成功跳转

import cn.lw.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/successServlet")
public class successServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getAttribute("user");
        if(user != null){
            response.setContentType("type/html;charset=utf-8");
            response.getWriter().write("登入成功,"+ user.getUser()+"欢迎你!");
        }
    }
}

用户信息类User.java

public class User {
    private int id;
    private String username;
    private String password;

    public User() {
    }

    public User(int id, String user, String password) {
        this.id = id;
        this.username = user;
        this.password = password;
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String user) {
        this.username = user;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", user='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

数据库操作:Daouser.java

import cn.lw.domain.User;
import cn.lw.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class Daouser {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    public User loginUser(User user_login){
        try {
            String sql = "select * from users where user =? and password=?";

            User user = jdbcTemplate.queryForObject(sql,
                    new BeanPropertyRowMapper<User>(User.class),
                    user_login.getUsername(),user_login.getPassword());
            return user;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值