來源:三月的靈夢 發(fā)布時間:2018-11-23 15:40:24 閱讀量:1035
本次項目整體思路:首先在login.jsp中寫一個用戶名和密碼的登陸界面跳轉(zhuǎn)到LoginServlrt進行數(shù)據(jù)組裝login然后調(diào)用LoginDao進行校驗login():登陸校驗,LoginDao通過返回值,返回登陸結(jié)果 ,LoginServlet根據(jù)不同的結(jié)果進行不同的處理。登陸成功跳過去Welcome.jsp,登陸失敗跳回login.jsp
eclipse中創(chuàng)建一個web項目之后創(chuàng)建一個jsp的文件名字叫l(wèi)ogin.jsp 寫好login界面
<title>登陸</title>
<form action="LoginServlet" method="post">
用戶名 <input type="text" name="uname">
密碼 <input type="password" name="upwd">
<input type="submit" value="登陸">
</form>
1
2
3
4
5
6
2.然后在創(chuàng)建一個JAVAbean的實體類(Login)用來封裝用戶名和密碼
package com.entiry;
public class Login {
int id;
String uname;
String upwd;
public Login() {
}
public Login( String uname, String upwd) {
this.uname = uname;
this.upwd = upwd;
}
public Login(int id, String uname, String upwd) {
this.id = id;
this.uname = uname;
this.upwd = upwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
3.接著需要寫一個連接數(shù)據(jù)庫的類用來實現(xiàn)校驗登陸(用于處理數(shù)據(jù))我用的mysql,你們用之前需要創(chuàng)建好表。(下面有我關(guān)于這次項目的數(shù)據(jù)表)
LoginDao類:
package com.Dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.entiry.Login;
//模型層 用于處理數(shù)據(jù)
public class LoginDao {
public static boolean login(Login login) {
Connection connection =null;
PreparedStatement pstmt=null;
ResultSet rs=null;
int s=-1;
boolean flag;//true為登陸成功 false為登陸失敗
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
String sql="select * from login where Uname=? and Upwd=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1,login.getUname() );
pstmt.setString(2, login.getUpwd());
rs = pstmt.executeQuery();
if(rs.next()){
s=rs.getInt(1);
}
if(s>0){
flag=true;//登陸成功
}
else{flag=false;//登陸失敗
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
flag=false;//登陸失敗
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag=false;
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag=false;
}finally{
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(connection!=null)connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
4.最后在創(chuàng)建一個servlet類做到承上啟下的作用
LoginServlet類
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Dao.LoginDao;
import com.entiry.Login;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//處理登陸請求
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
Login login=new Login(name ,pwd);
//調(diào)用登陸功能
LoginDao.login(login);
boolean s=LoginDao.login(login);
if(s==true){
response.sendRedirect("Welcome.jsp");//登陸成功跳轉(zhuǎn)到Welcome.jsp界面
}
else{response.sendRedirect("login.jsp");//登陸失敗跳回loginjsp界面繼續(xù)登陸
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
5.寫一個Welcome.jsp的JSP用于登陸成功的顯示
<title>登陸成功</title>
</head>
<body>
登陸成功!
</body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
mysql數(shù)據(jù)表:創(chuàng)建一個庫test 在test中創(chuàng)建一個叫l(wèi)ogin的表
這就是整個項目的完成步驟,到這里整個項目就寫完。
---------------------