Home>

login.jsp-the input file for the view section

success.jsp-output file for the view part

failure.jsp-output file for the view section

loginbean.java-model part

loginservlet.java-controller section

web.xml-configuration file for web applications

The following are introduced separately:

1.login.jsp

The input file of the function,The user accesses this file first.It is mainly used to enter user name and password.

code show as below:

<%@page contenttype="text/html;charset=gb2312"%>
<script language="javascript">
 function isvalidate (form)
 {
   //get the information entered by the user
   username=form.username.value;
   userpass=form.userpass.value;
   //determine the length of the username
   if (! minlength (username, 6))
   {
     alert ("The username is less than 6 characters long!");
     form.username.focus ();
     return false;
   }
   if (! maxlength (username, 8))
   {
     alert ("The username is longer than 8 characters!");
     form.username.focus ();
     return false;
   }
   //determine the password length
  if (! minlength (userpass, 6))
   {
     alert ("Password length is less than 6 characters!");
     form.userpass.focus ();
     return false;
   }
   if (! maxlength (userpass, 8))
   {
     alert ("Password is longer than 8 characters!");
     form.userpass.focus ();
     return false;
   }
   return true;
 }
 //verify that the minimum length is met
 function minlength (str, length)
 {
   if (str.length>= length)
     return true;
   else
     return false;
 }
 //determine if the maximum length is met
 function maxlength (str, length)
 {
   if (str.length<= length)
     return true;
   else
     return false;
 }
</script>
<html>
 <head>
  <title>User Login</title>
 </head>
 <body>
  <h2>User Login</h2>
  <form name="form1" action="login" method="post"
    onsubmit="return isvalidate (form1)">
    Username:<input type="text" name="username"><br>
    Password:<input type="password" name="userpass"><br>
    <input type="reset" value="reset">
    <input type="submit" value="submit"<br>
  </form>
 </body>
</html>

Client authentication is provided in the code (username and password are 6-8 digits long). After the validation is passed, the request is submitted to the controller servlet.

2.success.jsp

After successful login, it will jump to this interface,The interface code is as follows:

<%@page contenttype="text/html;charset=gb2312"%>
<html>
 <head>
  <title>login success</title>
 </head>
 <body>
  <h2>${sessionscope.username} Hello, welcome to the online bookstore!</h2>
 </body>
</html>

The code uses an expression language to display the logged-in user information on the street.

3.failure.jsp

After login fails, it will jump to this interface,The interface code is as follows:

<%@page contenttype="text/html;charset=gb2312"%>
<html>
 <head>
  <title>login failure</title>
 </head>
 <body>
  <h2>username or password is incorrect,Please<a href="login.jsp">login again!</a></h2>
 </body>
</html>

A hyperlink is provided in the code,Ability to link to the login interface.

4, loginbean.java

Complete the login function,It is assumed here that the username and password are equal to indicate a successful login.

package beans;
public class loginbean {
  public boolean validate (string username, string userpass) {
    return username.equals (userpass);
  }
}

5, loginservlet.java

The file completes control,The main functions can be described as follows:

①. Get the username and password entered by the user from login.jsp;

② Create an object of loginbean and call the method validate of loginbean;

③. According to the result returned by the method,Select success.jsp or failure.jsp to respond to the user.

The complete code is as follows:

package servlets;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet. *;
import javax.servlet.http. *;
import beans. *;
public class loginservlet extends httpservlet {
  public void doget (httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    dopost (request, response);
  }
  public void dopost (httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    //Get the user id and password entered by the user
    string username=request.getparameter ("username");
    string userpass=request.getparameter ("userpass");
    //create model object
    loginbean loginbean=new loginbean ();
    //call business method for verification
    boolean b=loginbean.validate (username, userpass);
    //file to redirect
    string forward;
    //If login is successful,Write the username into the session and go to success.jsp,    //otherwise go to failure.jsp
    if (b) {
    //get session
    httpsession session=(httpsession) request.getsession (true);
    //Save the username in the session
    session.setattribute ("username", username);
    //target redirection file is success.jsp
    forward="success.jsp";
    } else {
    //target redirection file is failure.jsp
    forward="failure.jsp";
    }
    //Get the dispatcher object
    requestdispatcher dispatcher=request.getrequestdispatcher (forward);
    //complete the jump
    dispatcher.forward (request, response);
  }
}

In the code, the user information of the logged-in user is stored in the session, which is also handled in the actual application.

Web.xml

The main code is the configuration of the servlet, the code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <servlet>
 <description>this is the description of my j2ee component</description>
 <display-name>this is the display name of my j2ee component</display-name>
 <servlet-name>loginservlet</servlet-name>
 <servlet-class>servlets.loginservlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>loginservlet</servlet-name>
 <url-pattern>login</url-pattern>
 </servlet-mapping>
</web-app>
jsp
  • Previous SQL Server database table records only retain N days of graphic tutorials
  • Next C ++ object often reference, dynamic creation and release related knowledge