JSP Login Verification Demo servlet, login, success_jsp programming

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.