jsp login verification demonstration servlet login success
- 2021-11-24 02:30:30
- OfStack
Login verification of jsp Demo
part_1: login. jsp: Login page:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Login.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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%
String fdbkMsg = (String) request.getAttribute("fdbkMsg");
if (null == fdbkMsg) {
fdbkMsg = "";
}
%>
<%
Boolean logedIn = (Boolean) session.getAttribute("logedIn");
if (null == logedIn) {
logedIn = false;
} else if (logedIn) {
// If you have logged in during this session, redirect directly to success-page-1
response
.sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp");
}
%>
<%
String username = "";
Cookie[] cookies = request.getCookies();
if ((null != cookies) && (cookies.length > 0)) {
for (Cookie c : cookies) {
if ("admin".equals(c.getValue())) {
username = "admin";
break;
}
}
}//end if-condition
%>
<body>
<br>
<div align="center">
Please log in :
<br>
<form action="/ServletDemoProject/servlet/LoginVerificationServlet"
method="post">
User name:
<input type="text" name="username" value="<%=username%>" />
<br>
Dense Code:
<input type="password" name="password" value="" />
<br>
<font color='red'><%=fdbkMsg%></font>
<br>
<input type="submit" value=" Submit " />
<br>
</form>
</div>
</body>
</html>
part_2: LoginVerificationServlet. java: Verify the login information. There is no connection to the database here. By default, only username: admin and password: 888888 are considered as successful login; When login fails: reforward to Login. jsp and prompt the user to log in failed and log in again;
package cn.mike.servlet.test_1209_Login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginVerificationServlet extends HttpServlet {
private static final long serialVersionUID = -6886327892796230543L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (("admin".equals(username)) && ("888888".equals(password))) {// Login Successful
// Save cookie To the client
Cookie userCookie = new Cookie("username", username);
userCookie.setMaxAge(60 * 2);// expiry : 2 minutes
response.addCookie(userCookie);
// Redirect to 1 A new page and prompt XXX The user logs in successfully using the session Access User Name) ;
request.getSession().setAttribute("username", username);
request.getSession().setAttribute("logedIn", true);
response
.sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp");
} else {// Log in failed
// Forward to the login interface and prompt with an error message:
request.setAttribute("fdbkMsg", " Wrong username or password! ");
request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward(
request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// do same as GET-method :
doGet(request, response);
}
}
part_3: success-page-1. jsp: Redirect to this page after verifying successful login, prompting the user that he has successfully logged in; If the user tries to use improper channels, e. g: accessing from the address bar will be forwarded to the login interface and prompted;
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success-page-1.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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%
String username = (String) session.getAttribute("username");
if (null == username) {
// If username Is null, indicating that it is not from the normal channel and forwarded to Login Page;
request.setAttribute("fdbkMsg", " Don't try to come in through the back door, log in quickly! ");
request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward(
request, response);
}
%>
<body>
<br>
<%=username%> Has successfully landed.
<br>
<font> You can choose to browse: </font>
<br>
<a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp"> Click here to have wonderful things .</a>
<br>
<a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp"> It's more exciting to click here .</a>
<br />
<a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp"> Do you dare to light here .</a>
<br />
</body>
</html>
part_4: success-page-2. jsp: Successful login page 2. If you have successfully logged in and saved your user name to session, you will check 1 when visiting this page to prevent violent access from the address bar;
<%@ page language="java" import="java.util.Date" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.text.SimpleDateFormat"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success-page-2.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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%
String username = (String) session.getAttribute("username");
if (null == username) {
request.setAttribute("fdbkMsg", " Heheda, is this where you come from? Log in! ");
// Forward to login interface :
request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward(
request, response);
}
SimpleDateFormat sDateFormat = new SimpleDateFormat("a");
Date today = new Date();
String am_pm_str = sDateFormat.format(today);
String am_pm_str_in_chinese = "";
if ("am".equalsIgnoreCase(am_pm_str)) {
am_pm_str_in_chinese = " Morning ";
} else
am_pm_str_in_chinese = " Afternoon ";
// set null;
sDateFormat = null;
today = null;
am_pm_str = null;
%>
<body>
<br />
<font><b><%=username%><%=am_pm_str_in_chinese%> OK, you can come to the page 2 It's not easy .</b>
</font>
</body>
</html>