JSP uses Servlet as the Controller to implement the MVC mode instance details, servletmvc

Source: Internet
Author: User

JSP uses Servlet as the Controller to implement the MVC mode instance details, servletmvc

This article describes how to use Servlet as a controller to implement the MVC mode in JSP. Share it with you for your reference. The details are as follows:

I. Objectives:

① A preliminary understanding of the MVC model;
② Master Servlet compilation;
③ Use MVC mode to complete logon.

Ii. Main content:

① Analyze the problems existing in the JSP + JavaBean mode, introduce the JSP + JavaBean + Servlet mode, and the relationship with the MVC mode;
② Introduce Servlet compilation, configuration, and operation through simple examples;
③ Use Servlet to control the logon function.

1. Is there a problem with the JSP + JavaBean mode?

The strength of JSP lies in interacting with people, that is, completing the input and output functions. However, in the JSP + JavaBean mode, JSP not only implements the input and output functions, but also controls the system (receives user requests, calls JavaBean, and then based on the call results, select the interface to respond to the user ). Therefore, in the third phase of JSP development, the control function is separated from JSP and implemented using Servlet to form the JSP + JavaBean + Servlet mode. JSP only completes the input and output, the JavaBean completes the processing, while the Servlet completes the control.

2. JSP + JavaBean + Servlet

JSP is responsible for input and output, and JavaBean is responsible for implementing business logic (functions) and Servlet control. This mode is also considered an implementation of the MVC mode. The MVC mode separates the system's business logic, control, and input/output. When developing applications, you can consider a specific part separately to simplify development.
V indicates the view, and the user-linked part. M indicates the model, the function is completed, and C indicates the controller. JSP usually acts as a view, JavaBean is a model, and Servlet acts as a controller.

3. What is Servlet?

Servlet is also a Web component. It has the same functions as JSP. Servlet is a pure Java file and a special Java class.
What is the difference between Servlet and JavaBean? All are Java classes, but Serlvet can receive user requests and the client can directly access them. However, JavaBean cannot be directly accessed by the client. It must be called by JSP or other Java files (including servlets.

4. Example: HelloServlet

The following is a Servlet. The complete function is to output a sentence "Servlet test! ".

1) file writing

Package servlet; // The package import java required for Servlet development. 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 to classpath and then compile.

3) Configuration

In the configuration file web. xml. The description and access method settings are as follows.

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

Access: http: // 127.0.0.1: 8080/ch6/hello

6. How can I use this mode to implement the login function?

Interface interface and response interface do not need to be modified, JavaBean to complete the processing, do not need to change, just use Servlet 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 {// Step 1: Get user input String username = request. getParameter ("username"); String userpass = request. getParameter ("userpass"); // Step 2: Call JavaBean User user = new User (); user. setUsername (username); user. setUserpass (userpass); boolean B = user. check (); // Step 3: Select an interface to respond to the user 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 attribute of the logon Interface

<% @ Page contentType = "text/html; charset = gb2312 "%> log on to the <br> <form name =" form1 "method =" post "action =" process "> User ID: <input type = "text" name = "username"> <br> password: <input type = "password" name = "userpass"> <br> <input type = "submit" value = "Logon"> <input type = "reset" value = "reset "> </form> <% @ include file =" contact. jsp "%>

7. Test Run

Access the logon interface, enter information, and then submit.
An error is reported, indicating that the request method is not supported. Add the following method to Servlet:

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

Because the Request Method on the JSP page is post, The doPost method must be provided in the Servlet. The method definition is the same as that of doGet. Here, you only need to call the doGet method to implement the method.

8. Servlet Methods

Init Method for initialization;
Service method: doGet method and doPost Method
Destroy method to release resources

9. Lifecycle

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

I hope this article will help you with JSP program design.

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.