Java Web Learning (30): Implementing User Login with Jsp+servlet+javabean

Source: Internet
Author: User
Tags stub

Implement User Login

User name admin, password admin, login successfully use the server internal jump to login_success2.jsp page, and prompt login successful use

Households If the login fails, jump to the login_failure2.jsp page.

Before we used JSP implemented, also Jsp+javabean implemented, this time we are more detailed, using Jsp+servlet+javabean implementation

Households Login, many of the source code is also written in the front.

Let's look at the overall directory structure first:

Take a look at the source code:

Entity class Users.java Source code:

Package Com.entity;import java.io.unsupportedencodingexception;/** * User entity class * @author Administrator * @date July 11, 2016 */p Ublic class Users {Private propertiesPrivate String username;private String password;No parameter constructorPublic Users () {}The setter and getter methods are obtained by refactoringPublic String GetUserName () {return username;}public void Setusername (String username) throws Unsupportedencodingexception {This.username = toUTF8 (username);}Public String GetPassword () {return password;}public void SetPassword (String password) {This.password = password;}private string ToUTF8 (String str) throws Unsupportedencodingexception {return new String (Str.getbytes ("iso-8859-1"), "UTF-8");}}

servlet layer Loginservlet.java Source code:

Package Com.servlet;import Java.io.ioexception;import Javax.servlet.servletconfig;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import com.entity.users;/** * Servlet Implementation class Loginservlet */@WebServlet ("/loginservlet") public class Loginservlet extends HttpServlet {Private static final long serialversionuid = 1L;        /** * @see httpservlet#httpservlet () */public Loginservlet () {super (); TODO auto-generated Constructor stub}/*** @see Servlet#init (servletconfig)*/public void init (ServletConfig config) throws servletexception {TODO auto-generated Method Stub}/*** @see Servlet#destroy ()*/public void Destroy () {TODO auto-generated Method Stub}/*** @see Httpservlet#doget (httpservletrequest request, httpservletresponse response)*/protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {TODO auto-generated Method StubDoPost (request, response);}/*** @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response)*/protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {TODO auto-generated Method StubRequest.setcharacterencoding ("UTF-8");//Prevent Chinese garbledUsers user = new users ();String username = request.getparameter ("username");String Password = request.getparameter ("password");User.setusername (username);User.setpassword (password);Save registered successful user object in Session objectRequest.getsession (). SetAttribute ("Loginuser", user);Determine if the user name is legitimateif (User.getusername (). Equals ("admin") &&user.getpassword (). Equals ("admin")) {Response.sendredirect ("login_success2.jsp");}else{Response.sendredirect ("login_failure2.jsp");}}}

loginbyservlet.jsp page Source:

<%@ 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" ><title> Login Screen </title><!--End of Page title--<!--Libraries--<link type= "Text/css" href= "Css/login.css" rel= "stylesheet"/><script type= "Text/javascript" src= "Js/jquery-1.3.2.min.js" ></script><script type= "Text/javascript" src= "Js/easytooltip.js" ></script><script type= "Text/javascript" src= "Js/jquery-ui-1.7.2.custom.min.js" ></script><!--End of Libraries-->

login_success2.jsp page Source:

<%@ 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" ><title>login_success page </title><!--End of Page title--<!--Libraries--<link type= "Text/css" href= "Css/login.css" rel= "stylesheet"/><script type= "Text/javascript" src= "Js/jquery-1.3.2.min.js" ></script><script type= "Text/javascript" src= "Js/easytooltip.js" ></script><script type= "Text/javascript" src= "Js/jquery-ui-1.7.2.custom.min.js" ></script><!--End of Libraries--><div class= "logo" ><a href= "#" ></a></div><div id= "box" >Welcome to <font color= "Red" ><jsp:getproperty name= "Loginuser" property= "username"/></font>, Login successful!</div></div></body>

login_failure2.jsp page Source:

<%@ 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" ><title>login_failure page </title><!--End of Page title--<!--Libraries--<link type= "Text/css" href= "Css/login.css" rel= "stylesheet"/><script type= "Text/javascript" src= "Js/jquery-1.3.2.min.js" ></script><script type= "Text/javascript" src= "Js/easytooltip.js" ></script><script type= "Text/javascript" src= "Js/jquery-ui-1.7.2.custom.min.js" ></script><!--End of Libraries--><div class= "logo" ><a href= "#" ></a></div><div id= "box" >Login failed! Please check the user or password!<br><a href= "loginbyservlet.jsp" > Back to login </a></div></div></body>

Operation Result:

Login success:


Login failed:


Java Web Learning (30): Implementing User Login with Jsp+servlet+javabean

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.