JSP实现用户登录、注册和退出功能(2)

使用JDBC连接数据库,如果用户名或密码为空时,还是跳转到登录页面login.jsp
如果用户名和密码不为空,进行连接数据库查询用户表,如果能够查询到记录,表示登录成功,将用户信息保存到session,跳转到欢迎页面welcome.jsp

如果根据用户名和密码查询不到记录,表示登录失败,重新跳转到登录页面login.jsp

4.3欢迎页面

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <title>My JSP 'welcom.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link type="text/css" href="https://www.jb51.net/styles.css"> --> </head> <body> <table> <tr> <td><img src="https://www.jb51.net/images/logo4.png" /> </td> <td><img src="https://www.jb51.net/images/logo2.png" /> </td> </tr> <tr> <td colspan="2"><hr /> </td> </tr> <tr> <td> <table> <tr> <td><a>Main</a> </td> </tr> <tr> <td><a>Menu1</a> </td> </tr> <tr> <td><a>Menu2</a> </td> </tr> <tr> <td><a>Menu3</a> </td> </tr> <tr> <td><a>Menu4</a> </td> </tr> <tr> <td><a>Menu5</a> </td> </tr> <tr> <td><a>Menu6</a> </td> </tr> <tr> <td><a>Menu7</a> </td> </tr> <tr> <td><a>Menu8</a> </td> </tr> </table></td> <td> <form action="loginout.jsp" method="post"> <table> <tr> <td colspan="2">登录成功!</td> </tr> <tr> <td>欢迎你,</td> <td>${username }</td> </tr> <tr> <td colspan="2"><input type="submit" value="退出" /></td> </tr> </table> </form></td> </tr> </table> </body> </html>

使用EL表达式展示用户信息

效果

JSP实现用户登录、注册和退出功能

4.4欢迎页退出逻辑处理页面

loginout.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <title>My JSP 'loginout.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link type="text/css" href="https://www.jb51.net/styles.css"> --> </head> <body> <% session.removeAttribute("username"); response.sendRedirect("https://www.jb51.net/login.jsp"); %> </body> </html>

将session的用户信息移除,跳转到登录页面login.jsp

4.5注册页面

register.jsp

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjpjy.html