Jsp+javabean+servlet Working principle Example explanation

Source: Internet
Author: User
Tags client

The development of Jsp+javabean two-storey structure should be more familiar and better understood.

But one thing must be clear is the user through the browser to send a Web page request, the request to the server after the server to find the corresponding page, if it is the first request (the second do not have to explain the implementation), for JSP to generate a servlet, The servlet engine is then used to execute the servlet, embedding the results of the call JavaBean into the browser that is returned to the user in the page.

The essence of the Jsp+javabean+servlet three-tier structure is a controller:servlet to distribute the client browser's request. It would be helpful to understand the servlet if the role of the servlet acting as a controller is understood as preprocessing of the client's request. The Web.xml profile allows you to find the corresponding relationship between a user request and a particular servlet, each of which has a specific servlet object, so that the processing user requests is a Servlet object that inherits from HttpServlet.

<!--jspc servlet mappings start
<servlet>
<servlet-name>ms1</servlet-name
<servlet-class>news. Firstaction</servlet-class>
</servlet>
<servlet>
<servlet-name>ms2</servle T-name>
<servlet-class>news. Detailaction</servlet-class>
</servlet>
<!--jspc servlet mappings end;
<servlet-m Apping>
<servlet-name>ms1</servlet-name>
<url-pattern>/newsmain</url-pattern>     
</servlet-mapping>
<servlet-mapping>
<servlet-name>ms2</servlet-name>
<url-pattern>/newsdetail</url-pattern>
</servlet-mapping>

As shown above, a section of the configuration servlet from Web.xml, the first part is primarily used to configure the servlet to be associated with a specific Servlet object, and the second part is primarily used to configure which servlet handles the request, the Servlet name Association, Processing a request is associated with a specific servlet processing object, such as a request from a client browser to/newsmain, which is handled by the MS1 servlet and can be found by MS1 to the corresponding Serlet object news. Firstaction, that is,/newsmain->ms1->news. Firstaction, this is the meaning of the configuration file. To now understand the user/newsmain request will be news. Firstaction class objects to deal with, so say, to understand the program to understand the role of firstaction is what on the line. For example, here is an implementation of firstaction.

public final class FirstAction extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 DB db = new DB();
 HttpSession session = req.getSession();
 try {
  session.setAttribute(Constants.NEWS_LIST_KEY, News
   .SearchNewsTitle(db));
 } catch (Exception e) {
  e.printStackTrace();
 }
 db.close();
 String target = "/P43_News/newsMain.jsp";
 resp.sendRedirect(target);
}
}

This implementation can be seen when the server receives a client request to perform news.searchnewstitle (DB) operation, and then puts the return value through Session.setattribute into the session, It is then indirectly transferred via Resp.sendredirect (target) to newsmain.jsp, so that the corresponding values stored in the session can be obtained through the Session.getattribute function in newsmain.jsp.

Looking back is easy to see jsp+javabean two-layer structure and jsp+javabean+servlet three-layer structure, two-tier structure must be placed in the JSP, such as News.searchnewstitle (db), The three-layer structure of the first preprocessing in the servlet, and then equivalent to the processing results through the session back to the JSP, so that JSP more attention to the display of the interface.

Related Article

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.