controller:
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
response.setContentType("text/html;charset=utf-8");
//設置請求編碼
request.setCharacterEncoding("utf-8");
//獲取用戶名和密碼
String userName = request.getParameter("userName");
String password = request.getParameter("password");
LoginService ls = new LoginServiceImpl();
User u = ls.checkLoginService(userName, password);
System.out.println(u);
//響應處理結果
if (u != null) {
response.getWriter().write("登錄成功,歡迎您:" + u.getU_name());
}else{
response.getWriter().write("登錄失敗");
}
}
}
service層:
public interface LoginService {
//判斷用戶是否輸入正確的信息
User checkLoginService(String userName,String password);
}
ServiceImpl層:
public class LoginServiceImpl implements LoginService {
// 創建Dao層對象
LoginDao ld = new LoginDaoImpl();
public User checkLoginService(String userName, String password) {
// TODO Auto-generated method stub
return ld.checkLoginDao(userName,password);
}
}
Dao層:
public interface LoginDao {
//判斷用戶輸入信息是否正確
User checkLoginDao(String userName, String password);
}
DaoImpl層:
public class LoginDaoImpl implements LoginDao{
@Override
public User checkLoginDao(String userName, String password) {
ResultSet rs = null;
PreparedStatement stat = null;
Connection con = null;
User u = null;
try {
//1.註冊驅動
Class.forName("com.mysql.jdbc.Driver");
//2.獲得連接
String url = "jdbc:mysql://localhost:3306/course";
String user = "root";
String password1 = "123456";
con = DriverManager.getConnection(url,user,password1);
String sql = "select * from WEB_USER WHERE U_NAME = '" + userName + "' and U_PWD = '"+password+"'";
//String sql = "SELECT * FROM USER_LOGIN WHERE U_NAME = ? AND U_PWD = ?";
// 創建sql命令對象
stat = con.prepareStatement(sql);
// 給佔位符賦值
//stat.setString(1,userName);
//stat.setString(2,password);
// 執行
rs = stat.executeQuery(sql);
//6.處理結果集resulrset boolean next 返會true,有結果集,返回false則沒有
while(rs.next()){
u = new User();
u.setU_id(rs.getInt("U_ID"));
u.setU_name(rs.getString("U_NAME"));
u.setu_pwd(rs.getString("U_PWD"));
u.setu_email(rs.getString("U_EMAIL"));
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(rs != null){
try {
rs.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stat != null){
try {
stat.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("---");
return u;
}
}
數據庫建表的sql語句手欠刪掉了
實體類對象如下:
public class User {
private int u_id;
private String u_name;
private String u_pwd;
private String u_email;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int u_id, String u_name, String u_pwd, String u_email) {
super();
this.u_id = u_id;
this.u_name = u_name;
this.u_pwd = u_pwd;
this.u_email = u_email;
}
public int getU_id() {
return u_id;
}
public void setU_id(int u_id) {
this.u_id = u_id;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public String getu_pwd() {
return u_pwd;
}
public void setu_pwd(String u_pwd) {
this.u_pwd = u_pwd;
}
public String getu_email() {
return u_email;
}
public void setu_email(String u_email) {
this.u_email = u_email;
}
@Override
public String toString() {
return "User [u_id=" + u_id + ", u_name=" + u_name + ", u_pwd=" + u_pwd + ", u_email=" + u_email + "]";
}
}
前端的登錄界面:
<html>
<head>
<meta charset="UTF-8">
<title>登錄</title>
<style type="text/css">
*{
margin:0px;
padding:0px;
font-size:16px;
}
li{
list-style-type:none;
}
.loginModel{
width:500px;
margin:50px auto;
padding:20px;
border:1px solid green;
border-radius:10px;
}
h2{
text-align:center;
}
.loginModel li {
margin:40px 0px;
}
.loginModel li h2{
font-size:28px;
}
.loginModel li input{
width:300px;
height:40px;
line-height:40px;
vertical-align:middle;
}
.loginModel li .inputinfo{
width:100px;
display:inline-block;
}
.loginModel .submitline{
text-align:center;
}
.promptinfo{
color:red;
}
</style>
<script type="text/javascript" src="./jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#userName").keyup(function(){
checkLogin();
});
//判斷登錄信息是否符合格式
function checkLogin(){
console.log("hduiid")
if(!/\s{2,}/.test($("#userName").val())){
$(".promptinfo").text("請輸入兩位數以上的用戶名");
}
}
});
</script>
</head>
<body>
<!-- 登陸模塊 -->
<form action = "/Land/LoginServlet" method ="post">
<ul class = "loginModel">
<li>
<h2>歡迎登陸</h2>
</li>
<li>
<span class ="inputinfo">用戶名:</span>
<input type="text" name="userName" id="userName" placeholder="請輸入用戶名">
</li>
<li>
<span class = "inputinfo">密碼:</span>
<input type = "password" name = "password" id = "password" placeholder ="請輸入密碼">
</li>
<li class = "promptinfo">
</li>
<li class="submitline">
<input type="submit" value="登錄">
</li>
</ul>
</form>
</body>
</html>