Introduction to MVC

Source: Internet
Author: User

As we have written a lot above, we can see that servlet is good at completing actual programming tasks, such as operating HTTP status code and headers, using cookies, tracking sessions, saving information across requests, and compressing pages, access the database. However, the HTML generated by Servlet is very lengthy and difficult to read, so JSP is introduced to complete the presentation layer task.

Based on this idea, MVC separates the presentation layer from the business logic layer and gives full play to its strengths.

Bean indicates the corresponding result (model), JSP indicates the page (view), Servlet calls the business logic and Data Processing (Controller ).

Dynamic calling of code policies in JSP.

The main driving force of the MVC scheme is the desire to separate the code for operating data from the code for expressing data. In very complex applications, more sophisticated MVC frameworks will be more advantageous. The most popular of these frameworks is Apache structs. for medium and simple solutions, using requestdispatcher to implement MVC from scratch is more intuitive and flexible. Here we mainly understand requestdispatcher and the idea of MVC.

1. Implement MVC with requestdispatcher

1) define bean to represent data

2) use servlet to process requests

Servlet Read Request Parameters

3) Enter Bean

The servlet calls the commercial logic or data access code to obtain the final result and put the result in the bean.

4) store the bean in the request, session, or servlet context

ValueObject value = new ValueObject(...);
request.setAttribute("key", value);


<jsp:useBean id="key" type="somePackage.ValueObject"
scope="request" />

Or:

ValueObject value = new ValueObject(...);
HttpSession session = request.getSession();
session.setAttribute("key", value);



<jsp:useBean id="key" type="somePackage.ValueObject"
scope="session" />

 
Or:

ValueObject value = new ValueObject(...);
getServletContext().setAttribute("key", value);


<jsp:useBean id="key" type="somePackage.ValueObject"
scope="application" />

5) forward requests to the JSP page

The forward request uses the forward method of requestdispatcher. To obtain requestdispatcher, call the getrequestdispatcher method of servletrequest and provide the relative address:

Requestdispatcher dispatcher = request. getrequestdispatcher (Address );

After obtaining the dispatcher object, you can call the forward method to transfer the control to the relevant address and provide httpservletrequest and httpservletresponse as parameters. The forward method is different from the sendredirect method. The forward method does not introduce additional responses and requests, so the URL displayed to the customer during use will not change.

RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);

6) extract data from Bean
After the request arrives at the JSP page, use JSP: usebean, JSP: getproperty to extract data

2. General example

1) Request-based data sharing

Servlet:

Valueobject value = new valueobject (...);

Request. setattribute ("key", value );

Requestdispatcher dispatcher = request. getrequestdispatcher ("/WEB-INF/somepage. jsp ");

Dispatcher. Forward (request, response );

JSP:

<JSP: usebean id = "key" type = "somepackage. valueobject" Scope = "request"/>

<JSP: getproperty name = "key" property = "someproperty"/>

2) Session-based data sharing

Servlet:

Valueobject value = new valueobject (...);

Httpsession session = request. getsession ();

Session. setattribute ("key", value );

Requestdispatcher dispatcher = request. getrequestdispatcher ("/WEB-INF/somepage. jsp ");

Dispatcher. Forward (request, response );

JSP:

<JSP: usebean id = "key" type = "somepackage. valueobject" Scope = "session"/>

<JSP: getproperty name = "key" property = "someproperty"/>

3) Application-based data sharing

Servlet:

Synchronized (this ){

Valueobject value = new valueobject (...);

Getservletcontext (). setattribute ("key", value );

Requestdispatcher dispatcher = request. getrequestdispatcher ("/WEB-INF/somepage. jsp"); dispatcher. Forward (request, response );

}

JSP:

<JSP: usebean id = "key" type = "somepackage. valueobject" Scope = "application"/>

<JSP: getproperty name = "key" property = "someproperty"/>

In fact, bean is stored in different locations, and the application scenarios are different for different methods.

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.