Java Web implementation of basic MVC instance analysis

  • 2021-01-18 06:36:13
  • OfStack

This article illustrates the basic MVC implementation of Java Web with examples. Share with you for your reference. The details are as follows:

login. jsp -- The input file for the view section
success. jsp -- The output file for the view section
failure. jsp -- The output file for the view section
LoginBean.java -- Model section
LoginServlet.java -- The controller part
web.xml -- The configuration file for web applications

The following are introductions:

1, login. jsp

The input file for this function is the file that the user first accesses. Mainly used to enter the user name and password.
The code is as follows:


<%@ 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 username length 
   if(!minLength(username,6))
   {
     alert(" User name length is less than 6 A!!!! ");
     form.username.focus();
     return false;
   }
   if(!maxLength(username,8))
   {
     alert(" The user name length is greater than 8 A!!!! ");
     form.username.focus();
     return false;
   }
   //  Password length determination 
  if(!minLength(userpass,6))
   {
     alert(" Password length less than 6 A!!!! ");
     form.userpass.focus();
     return false;
   }
   if(!maxLength(userpass,8))
   {
     alert(" Password length greater than 8 A!!!! ");
     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 whether the maximum length is satisfied 
 function maxLength(str,length)
 {
   if(str.length<=length)
     return true;
   else
     return false;
 }
</script>
<html>
 <head>
  <title> The user login </title>
 </head>
 <body>
  <h2> The user login </h2>
  <form name="form1" action="login" method="post"
    onsubmit="return isValidate(form1)">
     User name: <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-side authentication is provided in the code (the username and password are 6-8 bits long). After validation, the request is submitted to controller Servlet.

2, success jsp

After successful login, you will jump to this interface. The code of the interface is as follows:


<%@ page contentType="text/html;charset=gb2312"%>
<html>
 <head>
  <title> Login successful </title>
 </head>
 <body>
  <h2>${sessionScope.username} Hello, welcome to log in the online bookstore! </h2>
 </body>
</html>

The code uses expression language to display the user information after logging in on the street.

3, failure jsp

If you fail to login, you will be redirected to this interface. The code of the interface is as follows:


<%@ page contentType="text/html;charset=gb2312"%>
<html>
 <head>
  <title> Login failed </title>
 </head>
 <body>
  <h2> User name or password is incorrect, please <a href="login.jsp"> Log in again! </a></h2>
 </body>
</html>

The code provides a hyperlink, can link to the login screen.

4, LoginBean. java

Complete the login function, assuming that the user name 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

This file completes the control and the main functions can be described as follows:

(1). From login jsp get the user input user name and password;
(2) Create LoginBean object, call validate method LoginBean;
(3) Select success.jsp or failure.jsp to respond to the user based on the results returned by the method.

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 {
    //  The user who gets the user's input ID And password 
    String username = request.getParameter("username");
    String userpass = request.getParameter("userpass");
    //  Creating Model Objects 
    LoginBean loginBean = new LoginBean();
    //  The business method is called for validation 
    boolean b = loginBean.validate(username,userpass);
    //  The file to be directed to 
    String forward;
    //  If the login is successful, write the user name session And turn success.jsp . 
    //  Otherwise to failure.jsp
    if(b){
    //  To obtain session
    HttpSession session = (HttpSession)request.getSession(true);
    //  Save the user name to session In the 
    session.setAttribute("username",username);
    //  The target steering file is success.jsp
    forward = "success.jsp";
    }else{
    //  The target steering file is failure.jsp
    forward = "failure.jsp";
    }
    //  To obtain Dispatcher object 
    RequestDispatcher dispatcher = request.getRequestDispatcher(forward);
    //  Complete jump 
    dispatcher.forward(request,response);
  }
}

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

6, web. xml

The main code is Servlet configuration, 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>

Hope this article described to everybody JSP program design is helpful.


Related articles: