JSP实现简单登录
写在前面
在接触了JSP后,跟着老师简单做了一个小demo,模拟的登录操作
过程描述
- 首先是一个登录页面,通过表单接收数据;
- 将得到的数据封装进request对象,封装的内容为表单填写内容;
- 通过表单的method属性,用post方法发送到action指定的jsp文件,即检查用户信息的文件;
- 在检查文件连接数据库,并取出request传来的数据,传入sql语句进行数据库的匹配;
- 进入判断逻辑。判断成功,则通过jsp的forward page方法跳转到登陆成功页面,并携带参数;或是判断失败,跳转至失败页面。
代码
login.jsp
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 > <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <html>
> <head>
> <title>Login</title>
> </head>
> <body>
> <div style="text-align: center">
> <h1>Login</h1>
> <hr>
> <form action="login_check.jsp" method="post">
> <table border="1">
> <tr>
> <td colspan="2">Users Login</td>
> </tr>
> <tr>
> <td>Login ID: </td>
> <td><input type="text" name="id"></td>
> </tr>
> <tr>
> <td>Password: </td>
> <td><input type="password" name="password"></td>
> </tr>
> <tr>
> <td colspan="2">
> <input type="submit" value="Submit">
> <input type="reset" value="Reset">
> </td>
> </tr>
> </table>
> </form>
> </div>
> </body>
> </html>
>
login_check.jsp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 > <%--
> Created by IntelliJ IDEA.
> User: li
> Date: 2020/5/9
> Time: 10:53
> To change this template use File | Settings | File Templates.
> --%>
> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <%@ page import="java.sql.*"%>
> <%@ page import="javax.xml.stream.events.StartDocument" %>
> <%@ page import="javax.print.attribute.standard.PresentationDirection" %>
> <html>
> <head>
> <title>Check</title>
> </head>
> <body>
>
> <%!
> public static final String DRIVER = "com.mysql.jdbc.Driver";
> public static final String URL = "jdbc:mysql://localhost:3306/logintest?characterEncoding=UTF-8&serverTimezone=GMT%2B8";
> public static final String USER = "####";
> public static final String PASSWORD = "####";
> %>
> <%
> Connection con = null; //数据库连接对象
> PreparedStatement pstmt = null; //数据库操作
> ResultSet res = null; //结果集
> boolean flag = false; //是否登录成功标志
> String name = null;
> %>
> <%
> try {
> Class.forName(DRIVER); //加载驱动
> try {
> con = DriverManager.getConnection(URL,USER,PASSWORD); //获取数据库连接
> } catch (SQLException e) {
> System.out.println(e.toString());
> }
> String sql = "SELECT name FROM admininfo WHERE id=? AND password=?";
> /*这个语句中的?表示占位符,相当于一个未知量,通过下面的setString方法,可以填充占位符的值*/
>
> pstmt = con.prepareStatement(sql); //实例化数据库操作对象
>
> /*这里的request是从login.jsp页面表单提交的数据,经过封装,传递过来,分别有两个参数 id和password*/
> /*通过request带有的方法可以分别获取到传递的数据*/
> try {
> pstmt.setString(1,request.getParameter("id"));
> pstmt.setString(2,request.getParameter("password"));
> } catch (SQLException e) {
> System.out.println(e.toString());
> }
>
> res = pstmt.executeQuery(); //此语句只能查询,若要执行insert delete update等操作 须使用execute方法
>
> if(res.next()){
> name = res.getString(1); //参数1表示取第一列值
> flag = true;
> }
> } catch (ClassNotFoundException e) {
> System.out.println(e.toString());
> }
>
> //finally语句是在try的return语句执行之后,return返回之前执行
> finally {
> try {
> if(res != null){
> res.close();
> }
> if(pstmt != null){
> pstmt.close();
> }
> if(con != null){
> con.close();
> }
> } catch (SQLException e) {
> System.out.println(e.toString());
> }
> }
> %>
>
> <%
> if(flag){
> %>
> <jsp:forward page="login_success.jsp">
> <jsp:param name="username" value="<%=name%>"/>
> </jsp:forward>
> <%
> }else{
> %>
> <jsp:forward page="login_failure.jsp"></jsp:forward>
> <%
> }
> %>
>
> </body>
> </html>
>
>
login_success.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 > <%--
> Created by IntelliJ IDEA.
> User: li
> Date: 2020/5/9
> Time: 10:53
> To change this template use File | Settings | File Templates.
> --%>
> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <html>
> <head>
> <title>Login success</title>
> </head>
> <body>
> <h1>Login success!</h1>
> <h2>Welcome!
> <span style="color: red">
> <%=request.getParameter("username")%>
> </span>
> </h2>
> </body>
> </html>
>
>
login_failure.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 > <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <html>
> <head>
> <meta charset="UTF-8">
> <title>Failure</title>
> </head>
> <body>
> <h1>Login failure</h1>
> <h2>
> Please try again!
> <a href="login.jsp" style="color: red">Click here!</a>
> </h2>
> </body>
> </html>
>
遇到的问题
在数据库连接关闭的时候需要加入判断逻辑,判断为非空才能正常关闭;
需要项目导入mysql-connector-java-8.0.20.jar包,首先要配置到项目下,然后再添加到Tomcat的lib库中。并且要注意版本的匹配问题,否则会导致连接失败。
项目中我的各个版本为
1
2
3
4
5 > Mysql: 8.0.18
> Tomcat: 9.0.34
> connetion-jar包: 8.0.20
> jdk: 11.0.6
>
附上最后成功的截图
为什么要用Tomcat?
一般的,可以用html语言直接编写前端界面,并且通过js渲染,css美化。但这个过程是一个静态的,只是相当于把数据发送给浏览器进行解析,相当于面对客户端的。
而Tomcat是生成一个动态的页面,在html中嵌入jsp语言,生成的页面是通过服务器端发送出来。而Tomcat相当于一个中间的工具,帮助我们实现了这个过程,把我们自己的电脑变成了服务器端。
关于具体机制,下次学到再说 ☆( ̄▽ ̄)/$:*