JSP + JavaBean development of two-tier structure should be more familiar and relatively easy to understand.
However, one thing must be clear that users web browser to send the request, the request reaches the server on the server side to find the corresponding web page, if the first request (the second would not have interpreted a), for the JSP to said to generate Servlet, and then through the Servlet engine to implement Servlet, JavaBean to call the results embedded in the page returned to the user's browser.
JSP + JavaBean + Servlet three-tier structure is actually more of a Controller: Servlet to distribute the request of the client browser. If the role of the Servlet from the role of the controller to understand the client's request for the pre-process Servlet understanding will be of great help. Web.xml configuration file can be found by user requests and correspondence between a specific Servlet, each Servlet Servlet has a specific corresponding object, so that is a process the user request the Servlet objects inherited from HttpServlet.
ms1news.FirstActionms2news.DetailActionms1/newsmainms2/newsDetail
Web.xml as shown above is taken from a section of configuration servlet, the first part is mainly used to configure the Servlet object associated with a specific Servlet, the second part, which is used to configure the request by the Servlet handling, Servlet name associated with processing the request on Servlet processing associate specific objects, for example, the client browser sent / newsmain the request, which consists of ms1 servlet for processing, can be found through the ms1 corresponding serlet object news.FirstAction, that is / newsmain-> ms1- > news.FirstAction, this is the significance of the profile. Now know the user / newsmain request will be news.FirstAction class objects, so I said that to understand the role of procedure is necessary to understand FirstAction what is on the line. For example, an implementation of the following is 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) db.close (); String target = "/ P43_News/newsMain.jsp"; resp.sendRedirect (target);))
Through this realization can be seen, when the server receives a client requests the Executive News.SearchNewsTitle (db) of the operation, then the return value into the session by session.setAttribute years, then resp.sendRedirect (target) indirectly transferred to newsMain. jsp, it functions in newsMain.jsp where you can get through session.getAttribute stored in the corresponding value in the session.
Come back to it is easy to see that JSP + JavaBean two structures and JSP + JavaBean + Servlet three different structures, two structures must be carried out pretreatment on the JSP, such News.SearchNewsTitle (db), three structure in first pre conducted in the Servlet, then equivalent to the processing results through the Session returned to the JSP, JSP is more focused on the interface to the display.