JSP uses Servlet as controller to realize MVC mode example

  • 2021-08-17 00:39:21
  • OfStack

This paper illustrates how JSP uses Servlet as controller to realize MVC mode. Share it for your reference. The details are as follows:

1. Objectives:

① A preliminary understanding of MVC mode;
Grasp the writing of Servlet;
③ Use MVC mode to complete login function.

2. Main contents:

The problems of JSP+JavaBean are analyzed, and the relationship between JSP+JavaBean+Servlet and MVC is introduced;
2. Introduce the writing, configuration and operation of Servlet through simple examples;
Servlet is used to control the login function.

1. What are the problems of JSP+JavaBean?

The strength of JSP lies in its interaction with people, that is, its input and output functions. However, in the mode of JSP+JavaBean, JSP not only completes the functions of input and output, but also completes the control function of the system (receiving the user's request, calling JavaBean, and then selecting the interface to respond to the user according to the call result). Therefore, in the third stage of JSP development, the control function is separated from JSP and realized by Servlet, forming the mode of JSP+JavaBean+Servlet. JSP only completes input and output, JavaBean completes processing, and Servlet completes control.

2. JSP+JavaBean+Servlet

JSP is responsible for input and output, JavaBean is responsible for implementing business logic (function), and Servlet completes control. This pattern is also generally considered as an implementation of MVC pattern. MVC mode separates the business logic, control and input and output of the system. When developing applications, a certain part can be considered separately to simplify development.
V, which represents the view and the part that intersects with people, M, which represents the model and completes the function, C, which represents the controller. JSP usually acts as the view, JavaBean as the model, and Servlet as the controller.

3. What is Servlet

Servlet is also an Web component, which is the same as JSP in terms of completed functions. Servlet is a pure Java file and is a special Java class.
What is the difference between Servlet and JavaBean? They are all Java classes, but Serlvet can receive user requests and clients can access them directly. However, JavaBean cannot be accessed directly by clients and must be invoked by JSP or other Java files (including Servlet).

4. Example: HelloServlet

The following is an Servlet, and the completed function is to output a sentence "Servlet test! ".

1) Documentation


package servlet;
// Servlet Packages needed for development 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet
{
  public void doGet(HttpServletRequest request,HttpServletResponse response)
throw IOException,ServletException
  {
   response.setContentType("text/html;charset=gb2312");
   PrintWriter out = response.getWriter();
   out.print("Servlet Test! ");
  }
}

2) Compile

You need to configure C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/servlet-api. jar into classpath and compile.

3) Configuration

Write in the configuration file web. xml. It includes two parts: declaration and access mode setting.

Statement:


<servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>servlet.HelloServlet</servlet-class>
</serlvet>

Access method settings:


<servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/hello</url-pattern>
</servlet-mapping>

5. Test

Visit: http://127.0.0.1: 8080/ch6/hello

6. How to use this mode to realize login function?

There is no need to modify the interface and response interface, and JavaBean completes the processing and does not need to be changed. Only Servlet is used to replace the original login-process. jsp to complete the control function.

1) The reference code is as follows:


package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javabean.*;
public class LoginProcess extends HttpServlet
{
  public void doGet(HttpServletRequest request,HttpServletResponse response)
   throws IOException,ServletException
  {
   //  No. 1 1 Step: Get the user's input information 
   String username = request.getParameter("username");
   String userpass = request.getParameter("userpass");
   //  No. 1 2 Step: Call JavaBean
   User user = new User();
   user.setUsername(username);
   user.setUserpass(userpass);
   boolean b = user.check();
   //  No. 1 3 Step: Select 1 Interfaces respond to users 
   String forward;
   if(b)
     forward = "success.jsp";
   else
     forward = "failure.jsp";
   RequestDispatcher rd = request.getRequestDispatcher(forward);
   rd.forward(request,response);
  }
}

The above code basically shows the basic functions of Servlet as a controller.

2) Configuration


  <servlet>
   <servlet-name>process</servlet-name>
   <servlet-class>servlet.LoginProcess</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>process</servlet-name>
   <url-pattern>/process</url-pattern>
  </servlet-mapping>

3) Modify the action property of the login interface


<%@ page contentType="text/html;charset=gb2312"%>
 Please log in <br>
<form name="form1" method="post" action="process">
   Users ID : <input type="text" name="username"><br>
   Password: <input type="password" name="userpass"><br>
    <input type="submit" value=" Login "><input type="reset" value=" Reset ">
</form>
<%@ include file="contact.jsp"%>

7. Test run

Access the login interface, enter information, and then submit.
At this time, an error will be reported, prompting that the request mode is not supported. The following methods need to be added to Servlet:


public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
   doGet(request,response);
}

Because the request mode in JSP page is post, doPost method needs to be provided in Servlet, and the definition of method is the same as doGet, where the implementation of method only needs to call doGet method.

8. Main methods of Servlet

init method for initialization;
Service class methods: doGet method and doPost method
destroy method, releasing resources

9. Life cycle

After receiving the request, the server encapsulates the request information into an HttpServletRequest object and an HttpServletResponse object.
When accessing servlet for the first time, load the class, create the object, initialize (init method), and call the service class method (if it is get request, call doGet method, if it is post request, call doPost method).
Subsequent access, directly call the server class method.
When you uninstall Servlet, or when you shut down the server, call the destroy method to release resources.

I hope this paper is helpful to everyone's JSP programming.


Related articles: