当前位置:Gxlcms > 数据库问题 > JSP小例子——实现用户登录小例子(不涉及DB操作)

JSP小例子——实现用户登录小例子(不涉及DB操作)

时间:2021-07-01 10:21:17 帮助过:4人阅读

form name="regForm" action="dologin.jsp" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"></td> </tr> </table> </form>

当在login.jsp中输入用户名或密码并点击提交后,提交的表单将会传给一个用于验证的页面dologin.jsp。

dologin.jsp中的主要代码如下:

<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if ("admin".equals(username) && "admin".equals(password)) {
        request.getRequestDispatcher("login_success.jsp").forward(request, response);
    } else {
        response.sendRedirect("login_failure.jsp");
    }
%>

dologin.jsp页面用于验证用户名和密码是否正确(这里的验证方式即是用户名和密码是否都等于admin)。

如果登陆成功的话就会页面转发到一个login_success.jsp页面。
如果登录失败就会页面重定向到一个login_fail.jsp页面。

登陆成功的效果如下:

技术分享技术分享

 

登录失败的效果如下:

技术分享技术分享

代码:

技术分享
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>login page</title>
</head>
<body>
<form name="regForm" action="dologin.jsp" method="post">
<table>
  <tr>
    <td>用户名:</td>
    <td><input type="text" name="username"></td>
  </tr>
  <tr>
    <td>密码:</td>
    <td><input type="password" name="password"></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" value="提交"></td>
  </tr>
</table>
</form>
</body>
</html>
login.jsp 技术分享
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>do login page</title>
</head>
<body>
<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if ("admin".equals(username) && "admin".equals(password)) {
        request.getRequestDispatcher("login_success.jsp").forward(request, response);
    } else {
        response.sendRedirect("login_failure.jsp");
    }
%>
</body>
</html>
dologin.jsp 技术分享
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登陆成功</title>
</head>
<body>
登录成功!
</body>
</html>
login_success.jsp 技术分享
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录失败</title>
</head>
<body>
登录失败!
</body>
</html>
login_failure.jsp

 

JSP小例子——实现用户登录小例子(不涉及DB操作)

标签:

人气教程排行