DAY03-JAVAWeb项目之登陆验证
前端页面jsp
<html lang="zh-cmn-hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登陆</title>
<style>
body{
width: 100vw;
height: 100vh;
background-image: url("./imgs/background-image.jpg");
background-size: 1920px , 1080px;
text-align: center;
font-size: 16px;
min-width: 100px;
min-height: 600px;
}
form{
margin-top: 150px;
color:#fff;
}
h1{
padding-top: 65px;
color: #000;
}
.login{
position: relative;
width: 600px;
height: 500px;
margin: 0 auto;
background-color: rgba(255, 255, 255, 1);
}
.login input{
height: 40px;
width: 415px;
border: 1px solid rgb(203, 203, 203);
outline-color: rgb(180, 180, 180);
border-radius: 5px;
margin-top: 40px;
}
.login span{
display: inline-block;
width: 70px;
text-align: center;
color: #000;
}
#submit{
width: 30%;
position: relative;
right: -2%;
color: #fff;
font-size: 18px;
border: 1px solid #0381aa;
background-color: #00a7de;
}
#submit:hover{
background-color: #00bee7;
}
#Button{
background-color: #fff;
border: 1px solid #ccc;
width: 30%;
position: relative;
left: 11%;
font-size: 18px
}
#Button:hover{
background-color: #f7f7f7;
}
玩家登陆
用户名:<input type="text" placeholder="请输入你的用户名" name="username">
<br>
<span>密   码:</span>
<input type="password" placeholder="请输入你的密码" name="password">
<br>
<input id="submit" type="submit" value="登陆">
<input id="Button" type="button" value="注册">
</form>
</div>
</body>
</html>
后端Servlet验证
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
public void service(HttpServletRequest request , HttpServletResponse response) throws IOException {
System.out.println("LoginServlet");
/**
* 1.获取请求的参数
* 2.使用dbutils连接数据库
* 3.检查用户名,密码是否正确
* 4.登陆成功,显示用户所有信息
* 5.登陆成功,显示用户所有信息
* 6.登陆失败给错误提示
*/
//1.获取请求的参数
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("name = " + username + "password = " + password);
//2.使用dbutils连接数据库
JDBCUtil.getDs();
QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDs());
String sql = "select * from user where name = ? and pwd= ?";
User user = null;
try {
user = queryRunner.query(sql,new BeanHandler<User>(User.class), username , password);
System.out.println(user);
} catch (SQLException throwables) {
throwables.printStackTrace();
System.out.println("数据库连接失败");
}
//3.检查用户名,密码是否正确
if (user != null){
response.getWriter().write("login sucess"+user.getName());
}else{
response.getWriter().write("login fail");
}
}
}