JAVA web中MVC案列登陆(servlet)

来源:三月的灵梦 发布时间:2018-11-23 15:40:24 阅读量:1036

本次项目整体思路:首先在login.jsp中写一个用户名和密码的登陆界面跳转到LoginServlrt进行数据组装login然后调用LoginDao进行校验login():登陆校验,LoginDao通过返回值,返回登陆结果 ,LoginServlet根据不同的结果进行不同的处理。登陆成功跳过去Welcome.jsp,登陆失败跳回login.jsp


eclipse中创建一个web项目之后创建一个jsp的文件名字叫login.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.然后在创建一个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.接着需要写一个连接数据库的类用来实现校验登陆(用于处理数据)我用的mysql,你们用之前需要创建好表。(下面有我关于这次项目的数据表)

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;


//模型层 用于处理数据

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.最后在创建一个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);

//调用登陆功能

LoginDao.login(login);

boolean s=LoginDao.login(login);

if(s==true){

response.sendRedirect("Welcome.jsp");//登陆成功跳转到Welcome.jsp界面

}

else{response.sendRedirect("login.jsp");//登陆失败跳回loginjsp界面继续登陆

}


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数据表:创建一个库test 在test中创建一个叫login的表




这就是整个项目的完成步骤,到这里整个项目就写完。



--------------------- 



标签: PHP
分享:
评论:
你还没有登录,请先