版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
在本节教程中,我们完成项目的登录功能。
User
请在bean包中创建User类,代码如下:
package com.cn.bean;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class User {
private int id;
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
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 username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
图示如下:
登录页面
请在web文件夹下创建登录页面index.jsp,代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<style type="text/css">
body {
background: #e2e6c2;
}
#AllLogin {
margin-top: 15%;
text-align: center;
}
.loginDiv {
background: #fff;
padding: 20px 30px;
width: 400px;
margin: 0 auto;
text-align: center;
}
button {
width: 180px;
height: 40px;
background: #17334e;
border: 1px solid #fff;
color: #fff;
font-size: 14px;
}
input {
width: 260px;
padding: 4px 2px;
}
.tips {
width: 440px;
height: 30px;
margin: 5px auto;
background: #fff;
color: #17334e;
border: 1px solid #ccc;
display: none;
line-height: 30px;
padding-left: 20px;
font-size: 13px;
}
</style>
<script src="js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function () {
$("#username,#password").blur(function () {
var value = $(this).val();
if (value.trim() == '') {
$(".tips").css("display", "block")
$(".tips").html("输入内容不能为空!");
} else {
$(".tips").css("display", "none")
}
});
});
</script>
</head>
<body>
<div id="AllLogin">
<div id="tipsDivID" class="tips"></div>
<div class="loginDiv">
<form action="${pageContext.request.contextPath}/LoginServlet" method="post">
<p>
<label>用户名:<input id="username" type="text" name="username"></label>
</p>
<p>
<label>密 码:<input id="password" type="password" name="password"></label>
</p>
<p>
<button type="submit" id="login">登录</button>
</p>
</form>
</div>
</div>
</body>
</html>
图示如下:
LoginServlet
请在servlet包下创建LoginServlet处理用户登录。
- 登录成功:将当前用户保存至Session中再跳转到ShowStudentServlet查询所有学生并显示
- 登录失败:跳转到登录页面重新登录
代码如下:
package com.cn.servlet;
import com.cn.util.C3P0Utils;
import com.cn.bean.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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@WebServlet(name = "LoginServlet", urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = C3P0Utils.getConnection();
String sql = "SELECT * FROM user where username=? and password=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
boolean isSuc = resultSet.next();
if (isSuc) {
System.out.println("success");
User user = new User(username,password);
HttpSession session = request.getSession();
session.setAttribute("user",user);
String contextPath = request.getContextPath();
response.sendRedirect( contextPath+"/ShowStudentServlet");
} else {
System.out.println("error");
String contextPath = request.getContextPath();
response.sendRedirect(contextPath + "/index.jsp");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
C3P0Utils.release(connection, preparedStatement, resultSet);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
图示如下: