Almost all Java-based web apps require Model 2 (Part II)

zhaozj2021-02-12  141

Almost all Java-based web apps require Model 2

When the web container receives a request from the client program, the control stream begins to run. All requests are passed to the controller. Which view is delivered by the controller servlet. Figure 1 is only a view only, but in practical applications, there will be multiple views. The view of the client program request calls the method in the JavaBean and returns a REAPONSE object to the web container, passed the RESPONSE object to the client program by the web container.

The controller servlet uses the RESQUESTDISPATCHER object to push the request to their corresponding view (JSP page). One parameter in the URL will determine which view will be sent to the request, once we studied a simple case based on this mode, everything will It will become more clarified. From now on, you always remember to build your web application using the Model 2 architecture, unless your system is small and small in the future.

An example of a Model 2 application

This application I will discuss is a landing application system, the username / password has been hardly written in the system code. It consists of a servlet (as a controller) and two JSP pages (view), in order to see, we omitted the model section, but you can change this app, you can create a database table that stores the login name and password, then open one Check this user from a JDBC connection from the view to the database.

Controller servlets, such as Listing 1, a JSP page, a login.jsp (see Listing 2) another called Welcome.jsp (see Listing 3). When the user requests the default page of this app, login.jsp will Be the first to display, if the login success program will go to the Welcome.jsp page, if the login failed, the program jumps will login.jsp, and some error messages are displayed.

Among them, our most concerned part is how the controller decides to send the request to the appropriate JSP page. Let's take a look at the code in the service method of Listing1. The service method checks a parameter called login stored in the ServletRequest object. If the parameter is not discovered, the servlet will be pushed to the landing page:

IF (login == null) {requestDispatcher rd = request.getRequestDispatcher ("/ login.jsp"); rd.forward (request, response);}

Therefore, when the user first requests the application, the system will display the Login.jsp page due to the request object does not exist in the reluest object.

If the program finds a login parameter, the controller knows that the user is trying to log in. At this time, the servlet checks if the user name is "Taronga", and the password is "zoo". If so, servlet distributes requests to Welcome.jsp programs.

if (! password = null && userName.equals ( "Taronga") && password.equals ( "Zoo")) {// login successful RequestDispatcher rd = request.getRequestDispatcher ( "/ Welcome.jsp"); rd.forward (request ., response);

Otherwise, the application sets an attribute called Error in the ServletRequest object and assigns, and then returns the user to login.jsp. Due to this existence of this ignit, login.jsp displays an error message. Full text

Translated by windowsdna

转载请注明原文地址:https://www.9cbs.com/read-6193.html

New Post(0)