Import event-driven technology in JSP Servlet Development

Source: Internet
Author: User

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.

First, to implement event-driven, MVC technology must be used in JSP Servlet development.
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.
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 of "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 calls 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 "?? The void act (HttpServletRequest req, HttpServletResponse resp) 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:
1) Parse req.
2) initialize or update the corresponding class according to the resolution result in the previous step.
3) Call the corresponding method for logical processing.
4) reflect the processing result to the resp.
5) Specify the next displayed page based on the logic processing result
6) 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 ?? Message.

The Message class is the abstract of a 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. For 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. 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 modeled as a LoginMessage, And the LoginMessage is passed to the corresponding method. After processing, the corresponding image is specified and sent back to the client.

If the message instanceof ChangePasswordMessage is true, the message is modeled as ChangePasswordMessage, and ChangePasswordMessage is passed to the corresponding method. After processing, the corresponding image is specified and sent back to the client.

The above describes how to import the event-driven technology in JSP Servlet development.

  1. Aside from JSP, start with JSF.
  2. JSF and JSP are new partners
  3. Analysis of Application in JSP program
  4. Implement JSP page and code separation using JavaBean
  5. Integrate FCKEditor in JSF/JSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.