Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Model2 Application of Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, the editor will share with you the relevant knowledge points about how to achieve Java's Model2 application. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Almost all Java-based web app requires Model 2

When the Web container receives a request from the client program, the control flow starts to run. All requests are passed to the controller. It is up to the controller servlet to decide which view to pass the request to. Figure 1 lists only one view, but in practice, there are usually multiple views. The view requested by the client program calls the method in JavaBean and returns a Reaponse object to the web container, which passes the Response object to the client program.

The controller Servlet uses ResquestDispatcher objects to push requests to their corresponding view (JSP page), and a parameter in URL will determine which view to send the request to. Once we have studied a simple case based on this pattern, everything will become clearer. From now on, always remember to use the Model 2 architecture to build your web application, unless your system is small and will remain small in the future.

An example of application based on Model 2

The application I'm going to discuss is a login application where the user name / password has been rigidly written into the system code. It consists of a Servlet (as a controller) and two Jsp pages (views). For simplicity, we omitted the model section, but you can change the application, you can create a database table that stores the login name and password, and then open a JdbC connection from the view to the database to verify the user.

The controller Servlet is shown in ASPx "> Listing 1. On the Jsp page, one is called Login.jsp (see aspx" > Listing 2) and the other is called Welcome.jsp (see Listing 3). When the user requests the default page of the application, the Login.Jsp will be displayed first. If the login is successful, the program will go to the Welcome.jsp page. If the login fails, the program will jump to Login.jsp and display some error messages.

What we care most about is how the controller decides to send the request to the appropriate Jsp page. Let's look at the code in Listing1's Service method, which checks a parameter called login stored in the ServletRequest object. If the parameter is not found, Servlet pushes the request to the login page:

If (login==null) {

RequestDispatcher rd = request.getRequestDispatcher ("/ Login.jsp")

Rd.forward (request, response)

}

Therefore, when the user requests the application for the first time, the Login.Jsp page is displayed because there is no login parameter in the request object.

If the program finds the login parameter, the controller knows that the user is trying to log in to the system. At this point, Servlet will check whether the user name is "Taronga" and the login password is "Zoo". If so, Servlet distributes the request to the Welcome.jsp program.

If & userName.equals ("Taronga") & & password.equals ("Zoo") {

/ / login successful

RequestDispatcher rd = request.getRequestDispatcher ("/ Welcome.jsp")

Rd.forward (request, response)

}

Otherwise, the application sets a property called Error in the ServletRequest object, assigns it, and returns the user to Login.jsp. Due to the presence of this property called Error, Login.jsp displays an error message.

These are all the contents of the article "how to implement the Model2 Application of Java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report