The MVC model in the JAVA Web

Source: Internet
Author: User

After learning the servlet, the teacher explained an MVC model and upgraded the project we were practicing, before all of our code was written in the JSP, meaning there was a lot of Java code in the page. In the writing of the logic of the page is very convenient, write directly in the page, write the following can be used, but there is too much logic processing, this is a big problem, anyone can change the source code to change the results, so that the design of the page without any security.

The upgraded project uses the most common MVC model, that is, a request for a servlet, but so much code duplication, development efficiency is greatly reduced, on this basis, the teacher said an MVC model, the servlet as a controller, according to the request passed parameters to determine which method to implement, The approach is to put each request and the corresponding Java class into the map collection in the servlet's init () method, with each request, fetch the corresponding Java class from the map collection, and execute the corresponding Execute method. The parameters passed are HttpRequest and HttpResponse, as shown in:

One problem with this model is hard coding, and each time a method is added to the Java file to recompile, this development mode is not recommended to use, late maintenance is more troublesome.

On the basis of this model, the teacher also explained another updated MVC pattern, that is, the requested field and the corresponding Java class, method name, return value and so on in an XML file, the XML file of the first level element as a Java class, The attribute of element is used as the field for this Java class, and the Java class needs a map collection to add information for level two element. Level two element add two this class corresponding method return value, jump page and whether is forwarded, according to return value to determine which page to jump to, two level element also need to abstract into a Java Class (property is more than two when, also can not extract), convenient later to obtain the corresponding information. Using this method, the Digester tool class is used to automatically read the contents of the XML file, and the attribute of element is added to the corresponding class, and the root directory needs a map collection to obtain different Java classes based on different requests. When a request comes in, the corresponding Java class is obtained through the Get () method of the Map collection, and then the corresponding method name is obtained based on the properties of the class (which can be added to the XML file), and the corresponding method is invoked according to the principle of reflection. The return value type of the method is a string, and the return value of this method is a property of level two element, which, based on the Get () method of the map collection of this class, gets the level two element class, contains the target page, and also contains the forwarding or redirection In the servlet controller, you can determine whether to forward or redirect based on the properties of Class two. Such a servlet solves all the problem of access, and if you need to change the target page in the future, only need to change the configuration information in the XML file, this method realizes the soft coding of the software, which facilitates the extension and maintenance.

The XML file is as follows:

The init () method of the servlet is as follows:

?

The servlet service methods are as follows:

?

The specific design ideas are as follows:

The MVC model in the JAVA Web

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.