Basic concepts of MVC
MVC is not a stranger, including models, views, controllers, where models are used for the implementation of basic business logic, views are used to represent the results of responses, controllers are used for model control and request dispatch. Put on a diagram that introduces MVC components and features to understand the relationships and functions of three components:
Simple MVC implementation
The servlet runs in a container (Tomcat), and the container allows one component to invoke another. This is the example of the recommended beer in Head first servlet&jsp, which implements the basic idea of MVC, in which the Servlet is the controller, invokes the model and obtains the result information, saves it to the request object, and assigns the request to the JSP.
MVC Call Procedure:
- Client body request;
- The container receives the request and maps to the corresponding servlet based on the request URL and Web. xml;
- The servlet invokes the corresponding model and processes the actual business logic;
- After the model is processed, the result information is returned;
- The servlet encapsulates the result returned by the model as a property value into the Request object;
- Invoking the specified view (JSP, etc.) through the function of "request Assignment";
- The view invokes the result returned by the model in the Request object, and the generated page is returned to the container;
- The container feeds the results back to the client.
Front-End Submission Request page
The client can select colors in the beer feature as criteria for querying, such as:
The code is implemented as follows, the request data is submitted by post via form form declaration and processed by Selectbeer.do:
1 <HTML>2 <Body>3 <H1Align= "Center">Beer Selection Page</H1>4 <formMethod= "POST"5 Action= "Selectbeer.do">6Select Beer characteristics<P>7 Color:8 <Selectname= "Color"size= "1">9 <optionvalue= "Light">Light</option>Ten <optionvalue= "Amber">Amber</option> One <optionvalue= "Brown">Brown</option> A <optionvalue= "Dark">Dark</option> - </Select> - <BR><BR> the <Center> - <inputtype= "SUBMIT"> - </Center> - </form> + </Body> - </HTML>
Implementing a mapped servlet based on Web. xml
1 <Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"2 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3 xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee4 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "5 version= "3.1"6 Metadata-complete= "true">7 8 <servlet>9 <Servlet-name>Ch3 Beer</Servlet-name>Ten <Servlet-class>Com.example.web.BeerSelect</Servlet-class> One </servlet> A <servlet-mapping> - <Servlet-name>Ch3 Beer</Servlet-name> - <Url-pattern>/selectbeer.do</Url-pattern> the </servlet-mapping> - </Web-app>
A servlet corresponds to three names: URL name, deployment name, and fully qualified name. Where the URL name is client-facing, called by the client, the fabric signature is the secret internal name of the deployment service, can be defined arbitrarily; The fully qualified name is the corresponding real servlet. They are mapped and declared through <servlet> and <servlet-mapping>. The top URL is "/selectbeer.do", the deployment is named Ch3 Beer, and the security-qualified name is Com.example.web.BeerSelect.
The servlet controller that receives the request
1 PackageCom.example.web;2 3 Importjavax.servlet.*;4 Importjavax.servlet.http.*;5 ImportJava.io.*;6 ImportJava.util.*;7 ImportCom.example.model.*;8 9 Public classBeerselectextendshttpservlet{Ten One Public voidDoPost (HttpServletRequest request, httpservletresponse response)throwsIOException, servletexception{ A -String C = request.getparameter ("Color"); -Beerexport Beerexport =NewBeerexport (); thelist<string> result =Beerexport.getbrands (c); - -Request.setattribute ("Styles", result); - +RequestDispatcher view = Request.getrequestdispatcher ("result.jsp"); - View.forward (Request,response); + } A}
Beerservlet inherits from HttpServlet and can receive HTTP requests. The override Dopost method receives the form data, invokes the model to get the result, encapsulates the result into the request object, and calls result.jsp through the request dispatch to generate the page return.
Model
1 PackageCom.example.model;2 3 ImportJava.util.*;4 5 Public classbeerexport{6 7 PublicList<string>getbrands (String color) {8List<string> brands =NewArraylist<>();9 Switch(color) {Ten Case"Amber": OneBrands.add ("Jack Amber"); ABrands.add ("Red Moose"); - Break; - default: theBrands.add ("Jail Pale Ale"); -Brands.add ("Gout Stout"); - Break; - } + returnbrands; - } +}
A model is a generic class that implements specific business logic.
View
1<%@ pageImport= "Java.util.*"%>23</body>4<H1 align= "Center" > Beer Recommendations jsp5<p>6<%7List<string> brands = (List) request.getattribute ("Styles");8Iterator it =brands.iterator ();9 while(It.hasnext ()) {TenOut.print ("<br>try:" +It.next ()); One } A%> -</body> -A JSP is another servlet that simply removes the HTML that was originally written in the servlet and converts it to a servlet instantiation when the first request arrives. It will call the model in the request object to return the results, generating the page.
Page returned to the clientWhere Http://localhost:8080/Beer-v1/SelectBeer.do, Beer-v1 is the root of the Web application context.
Summarize
- The basic idea and calling process of MVC
- Simple MVC implementation
- Learn about servlet concepts: URL mapping, servlet instance implementations, and more.
Simple MVC understanding and implementation