Jsp login verification demo servlet, login, success, servletlogin

Source: Internet
Author: User

Jsp login verification demo servlet, login, success, servletlogin

Jsp login verification Demo

Part_1:Login. jsp: logon 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: Check the logon information. The database is not connected here. By default, only username: admin and password: 888888 are successfully logged on. If logon fails, the system forwards the information to Login again. jsp and prompts the user to log on failed. Log On 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 client Cookie userCookie = new Cookie ("username", username); userCookie. setMaxAge (60*2); // expiry: 2 minutes response. addCookie (userCookie); // redirect to a new page and prompt XXX User Logon success (access user name using session); 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 with error message: request. setAttribute ("fdbkMsg", "incorrect user name 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: Verify the login successful redirection to this page, prompting the user has been successfully logged in; if the user attempts to pass the improper way, e. g: access from the address bar will be forwarded to the logon 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"> 

Part_4: success-page-2.jsp: Login successful page 2, if you have successfully logged in to save the user name to the session, access to the page will be verified, to prevent brute force 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 all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.