Analysis on the working principle of JSP + JavaBean + Servlet Structure

Source: Internet
Author: User

The working principle of JSP + JavaBean two-layer structure should be familiar and understandable.

However, it must be clear that the user sends a webpage request through a browser. After the request arrives at the server, the user can find the corresponding webpage on the server, if this is the second request for the first time, you do not need to explain the execution). For JSP, You need to generate a Servlet and then execute the Servlet through the Servlet engine, embed the result of calling JavaBean into the page and return it to the user's browser.

The three-tier structure of JSP + JavaBean + Servlet is essentially an additional Controller: Servlet to distribute client browser requests. It will be of great help to understand the role of the controller Servlet as preprocessing of client requests. Through web. the xml configuration file can find the ing between user requests and specific servlets. Each Servlet corresponds to a specific Servlet object, therefore, a user request is a Servlet object inherited from HttpServlet.

 
 
  1. ﹤!-- JSPC servlet mappings start --﹥  
  2.     ﹤servlet﹥  
  3.         ﹤servlet-name﹥ms1﹤/servlet-name﹥  
  4.         ﹤servlet-class﹥news.FirstAction﹤/servlet-class﹥  
  5.     ﹤/servlet﹥  
  6.  
  7.     ﹤servlet﹥  
  8.         ﹤servlet-name﹥ms2﹤/servlet-name﹥  
  9.         ﹤servlet-class﹥news.DetailAction﹤/servlet-class﹥  
  10.     ﹤/servlet﹥ 
 
 
  1. ﹤!-- JSPC servlet mappings end --﹥  
  2.    ﹤servlet-mapping﹥  
  3.         ﹤servlet-name﹥ms1﹤/servlet-name﹥  
  4.         ﹤url-pattern﹥/newsmain﹤/url-pattern﹥  
  5.     ﹤/servlet-mapping﹥  
  6.  
  7.     ﹤servlet-mapping﹥  
  8.         ﹤servlet-name﹥ms2﹤/servlet-name﹥  
  9.         ﹤url-pattern﹥/newsDetail﹤/url-pattern﹥  
  10.     ﹤/servlet-mapping﹥ 

From web, as shown above. the configuration servlet in xml section. The first part is mainly used to configure the Servlet to be associated with a specific Servlet object. The second part is mainly used to configure the Servlet to process the request and the association of the Servlet name, the Processing request is associated with the specific Servlet processing object. For example, the request sent by the client browser/newsmain is processed by the ms1 servlet, and the corresponding serlet object news can be found through ms1. firstAction, that is,/newsmain-> ms1-> news. firstAction, which is the meaning of the configuration file. Now I understand that the user/newsmain request will be processed by objects in the news. FirstAction class. Therefore, to understand the program, you must understand what FirstAction is. For example, the following is an implementation of FirstAction.

 
 
  1. public final class FirstAction extends HttpServlet {  
  2.  protected void service(HttpServletRequest req, HttpServletResponse resp)  
  3.    throws ServletException, IOException {  
  4.  
  5.   DB db = new DB();  
  6.   HttpSession session = req.getSession();  
  7.  
  8.   try {  
  9.    session.setAttribute(Constants.NEWS_LIST_KEY, News  
  10.      .SearchNewsTitle(db));  
  11.   } catch (Exception e) {  
  12.    e.printStackTrace();  
  13.   }  
  14.  
  15.   db.close();  
  16.   String target = "/P43_News/newsMain.jsp";  
  17.   resp.sendRedirect(target);  
  18.  }  
  19.  

Through this implementation, we can see that when the server receives a client request to execute News. searchNewsTitle (db) operation, and then pass the returned value through session. setAttribute is put into the session, and then resp. sendRedirect (target) is indirectly transferred to newsMain. jsp in newsMain. through session in jsp. the getAttribute function can obtain the corresponding values stored in the session.

Looking back, we can easily see that the working principle of JSP + JavaBean is different from that of JSP + JavaBean + Servlet. The two-layer structure must put preprocessing in JSP, such as News. searchNewsTitle (db). The three-tier structure first pre-processes the data in Servlet, and then returns the processing result to JSP through Session, so that JSP is more focused on the display of the interface.

The above is the working principle of the JSP + JavaBean + Servlet structure. Do you know something about them?

  1. At the beginning of JSP Servlet Development
  2. Java Servlet API documentation
  3. Enhanced Servlet and JSP security without modifying code
  4. Configuration of JSP, Servlet, and Bean in Tomcat
  5. How to Improve Servlet and JSP Application Efficiency

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.