JSP-Servlet provides an excellent platform for Web application developers. However, most JSP-Servlet-based development in China is currently in the most basic state, with a low code reuse rate and low development efficiency, and cannot meet the requirements of large-scale and complex Web development.
For the event-driven technology imported into JSP-Servlet, the MVC technology must be used in JSP-Servlet development to implement event-driven.
◆ M = Model)
◆ V = View)
◆ C = Control)
View V) defines the user interface. Model M) defines the abstract class and logic. Control C) is responsible for completing business processing according to the logic of the event calling model of the view.
Average
◆ Model is implemented by common Java classes.
◆ View) is implemented by Jsp.
◆ Control) is implemented by Servlet.
Views and models are closely related and independent of each other.
A model is the abstract of a view and must contain certain logic to process the events of the objects presented by the view.
For the same logic, views can be expressed in various ways.
For example, in Web development,
[1] For a SingleSelection class that represents the abstract "single choice", it can be displayed as a Radio button, a single choice drop-down box, or any other control of single choice.
[2] for a Submition class that represents the "Submit" abstraction, it can be displayed as a Submit button or hyperlink or image in the user's browser, it can even be any control that can trigger the submit () method of JavaScript.
Event-driven is extremely important for MVC technology, mainly controlled by Control. The implementation of JSP-SERVLET itself is very suitable for writing event-driven programs.
As we all know, the javax. servlet. http. HttpServlet class is called every time.
DoPost (HttpServletRequest req, HttpServletResponse resp)
Or
DoGet (HttpServletRequest req, HttpServletResponse resp)
Method to respond to the submit operation on the client.
The Servlet RESPONSE event is the submit operation of the client browser. The client browser's submit event triggers the Servlet's specific event processing method, but there is only one entry: javax. servlet. http. HttpServlet service (HttpServletRequest req, HttpServletResponse resp) method. The specific response to the client operation may be the doPost (HttpServletRequest req, HttpServletResponse resp) or doGet (HttpServletRequest req, HttpServletResponse resp) method.
Therefore, you can override these methods to control Servlet response to submit events, and call different JSP, java bean, and even EJB.
For example, EventDrivenServlet inherits javax. servlet. http. httpServlet, and rewrite the doPost (HttpServletRequest req, HttpServletResponse resp) and doGet (HttpServletRequest req, HttpServletResponse resp) methods so that both methods call the same method directly, we name this method "act "??
Void act (HttpServletRequest req, HttpServletResponse resp)
The act method should call the corresponding classes and methods based on the information passed in by the req parameter, and finally return the resp to the client.
The act method must process the following items:
◆ Parse req.
◆ Initialize or update the corresponding class according to the resolution result in the previous step.
◆ Call the corresponding method for logical processing.
◆ Reflect the processing result to the resp.
◆ Specify the next displayed page based on the logic processing result
◆ Forward to the next page.
But how does the act method know which method of the class to be called and which screen to migrate?
The answer is to pass a Message to the act method.
For the event-driven technology imported into JSP-Servlet, the Message class is the abstract of the Message and should be defined as abstract. It can be an empty class. All specific Message classes are derived from this Message class. All Message classes must be inherited from the Message class.
As mentioned above, JSP on the screen) each submit control logically corresponds to a Submition, and each Submition is associated with a specific message class. In this way, as long as we can intercept these message classes at the Control layer, parse the information carried by these message classes, and delegate them to corresponding class methods for processing, event-driven.
Example:
There is a "login" button on the screen, which corresponds to a Submition class and generates a LoginMessage message. LoginMessage inherits the Message, adds a Hashtable member variable, and carries the username and password information.
There is also a "Change Password" button on the screen, which corresponds to a ChangePasswordSubmition class and generates a ChangePasswordMessage message. ChangePasswordMessage inherits the Message, adds a Hashtable member variable, and carries the username and password information.
For the event-driven technology imported into JSP-Servlet, when the Servlet responds to this submit, it can delegate processing based on the type and information carried by the sent message.
In actual implementation, we should overload the act () method of EventDrivenServlet and add a parameter of the Message class.
Void act (HttpServletRequest req, HttpServletResponse resp, Message message)
If the message instanceof LoginMessage is true, the message is converted into a LoginMessage and the LoginMessage is passed to the corresponding method, after the processing is complete, specify the corresponding image and return it to the client.
If the message instanceof ChangePasswordMessage is true, the message is modeled as ChangePasswordMessage and the ChangePasswordMessage is passed to the corresponding method, after the processing is complete, specify the corresponding image and return it to the client.
The above is a little explanation of the event-driven technology imported into JSP-Servlet. is it helpful to you?
- Functions and principles of several encodings in JSP and Servlet
- Use JSP include mechanism to improve appearance
- Storage and display of images in databases Based on JSP
- Methods for constructing JSP and Javabean development and release Environments
- JSP entry-level website environment setup steps