JSP's Login Verification 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" >
part_2: Loginverificationservlet.java: Verify logon information, there is no connection to the database, the default is only Username:admin, password:888888 login success, landing failure: Re-forwarding to the login.jsp and prompts the user to login failed, login 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 =-688632789279
6230543L; public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {Str
ing username = request.getparameter ("username");
String Password = request.getparameter ("password"); if (("admin". Equals (username)) && ("888888". Equals (password)) {//Login succeeded/save cookie to client cookie Usercookie = NE
W Cookie ("username", username);
Usercookie.setmaxage (* 2);//Expiry:2 minutes Response.addcookie (Usercookie);
Redirect to a new page and prompt the XXX user to log in successfully (using Session access username);
Request.getsession (). setattribute ("username", username); REquest.getsession (). setattribute ("Logedin", true);
Response. Sendredirect ("/servletdemoproject/login-demo/success-page-1.jsp"); else {//Login failed//forward to login interface and prompt for error message: Request.setattribute ("fdbkmsg", "username or password is wrong!")
");
Request.getrequestdispatcher ("/login-demo/login.jsp"). Forward (request, response); } public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexcept
Ion {//do same as get-method:doget (request, response); }
}
part_3: success-page-1.jsp: Verify that the login is redirected to the page, prompting the user to log in successfully; If the user attempts to pass the improper path, e.g: access from the Address bar, will be forwarded to the login interface, and make a prompt;
<%@ 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" >
Part_4:success-page-2.jsp: Landing Success Page 2, if the login has been successfully saved to the session, the page will be checked 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" >
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.