In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to import event-driven technology in JSP development Servlet, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
JSP-Servlet provides an excellent platform for Web application developers. However, at present, most of the domestic JSP-Servlet-based development is in the most basic state, with low code reuse rate and low development efficiency, which can not meet the requirements of large and complex Web development.
For importing event-driven technology into JSP-Servlet, first of all, to achieve event-driven, we must use MVC technology in JSP-Servlet development.
◆ M=Model (Model)
◆ V=View (View)
◆ C=Control (Control)
The view (V) defines the user interface. The model (M) defines abstract classes and logic. The control (C) is responsible for invoking the logic of the model according to the events that occur in the view to complete the business processing.
General
The ◆ Model is implemented by the normal Java class.
◆ View is implemented by Jsp.
◆ Control is implemented by Servlet.
Views and models are closely related and independent of each other.
A model is an abstraction of a view and must contain some logic to handle the events of the objects represented by the view.
At the same time, views can be represented in a variety of ways for the same logic.
For problems in importing an instance of event-driven technology in JSP-Servlet, for example, in Web development
[1] for a SingleSelection class that represents a "radio" abstraction, it can be displayed as a Radio button, a radio drop-down box, or any other radio control in the user's browser.
[2] for a Submition class that represents the "submit" abstraction, it can be displayed as Submit button, hyperlinks, pictures, or even any control that triggers the submit () method of JavaScript on the user's browser.
Event-driven is extremely important for MVC technology, which is mainly implemented by Control (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 calls the
DoPost (HttpServletRequest req, HttpServletResponse resp)
Or
DoGet (HttpServletRequest req, HttpServletResponse resp)
Method to respond to the client's submit operation.
The event that Servlet responds to is the submit action of the client browser. The submit event of the client browser triggers the specific event handling method of Servlet, but there is only one entry: the service (HttpServletRequest req, HttpServletResponse resp) method of the javax.servlet.http.HttpServlet class. (specific to the response to the client's operation, it may be doPost (HttpServletRequest req, HttpServletResponse resp) or doGet (HttpServletRequest req, HttpServletResponse resp) method.
So you can override these methods to control how Servlet responds to submit events, calling different JSP,JAVA BEAN or even EJB.
Example: EventDrivenServlet inherits javax.servlet.http.HttpServlet and overrides 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 class and method based on the information passed in the req parameter, and * * return the resp to the client.
The processing performed by the act method must include the following:
◆ parses req.
◆ initializes (or updates) the corresponding classes based on the parsing results of the previous step.
◆ calls the appropriate method for logical processing.
◆ reflects the results of the processing into resp.
◆ specifies the next page to be displayed according to the logical processing result
◆ forward to the next page.
But how does the act method know which method of which class to call and which screen to migrate to?
The answer is to pass a message Message to the act method.
For importing event-driven technology into JSP-Servlet, the Message class is an abstraction of the message, should be defined as abstract, and can be an empty class. All concrete message classes are derived from this Message class. All message classes must inherit from the Message class.
As mentioned earlier, every submit control on the screen (JSP) 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 in the Control layer, parse the information carried by these message classes, and delegate them to the methods of the corresponding classes to handle, we can implement event-driven.
Example:
There is a "login" button on the screen, which corresponds to a Submition class and generates a LoginMessage message. LoginMessage inherits Message and adds a Hashtable member variable with user name 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 Message and adds a Hashtable member variable with user name and password information.
For importing event-driven technology in JSP-Servlet, when the Servlet responds to the submit, it can delegate processing based on the type of message and the information it carries.
For 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)
For importing event-driven technology into JSP-Servlet, if message instanceof LoginMessage is true, then the message is modeled as LoginMessage, and the LoginMessage is passed to the corresponding method. After processing, the corresponding picture is specified and sent back to the client.
For importing event-driven technology into JSP-Servlet, if message instanceof ChangePasswordMessage is true, then the message is modeled as ChangePasswordMessage, and the ChangePasswordMessage is passed to the corresponding method. After processing, the corresponding picture is specified and sent back to the client.
These are all the contents of the article "how to import event-driven technology for Servlet in JSP development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.