1 Added Event Model: By setting the Listener, you can do some things before the servlet initialization. This is the application of the event model in the servlet.
After accessing / bookstore1 / enter, ContextListener constructs a DB and put it in servletContext so that bookstoreServlet can get this initialized DB from Context.
The core code is as follows:
Context = event.getServletContext ();
........................................
Context.setttribute ("BookDB", BookDB;
The implementation of the ContextListener class is very simple, only: ContextInitialized, ContextDestroyed two methods
Life event:
Events that are not single servletContext can be listened, and the events such as session can also listen, respectively: httpsessionListener, servletRequestListener
2 RequestDispatch:
Using RequestDispatch in BookStoreServlet, the code is as follows:
RequestDispatcher dispatcher =
GetServletContext (). getRequestDispatcher ("/ banner");
IF (Dispatcher! = NULL)
Dispatcher.include (Request, Response);
View Web.xml, know that / banner corresponds to the BannerServlet class.
A requestDispatch is an ordinary servlet class. Usually it is as follows: a small servlet class generates some Response results, and the other servlet contains this result, and constructs a response. This smaller servlet class can understand web pages Template. Component of the program, which improves the modularity of the servlet.
Read the source code for the first BookstoreServlet
3 At this point, there are two connections, one is the information of the book, one is start shopping
1) Click on the book BookDetailsServlet
Remove the bookDB from Context, find the record according to Parameter, then list the record. Some additions to the shopping cart and continue shopping.
1 Add to Cart CatalogServlet Set the shopping cart, and list the information to check the shopping cart, buy a book, and books.
2 Check the shopping cart ShowcartServlet checks according to the value of Request, nothing new
3 Other servlets are doing so.
2 should be handled so for httpservlets
1 get Output Street from Response
2 Filling the head information of the resp
3 Write body to OUTPUT
4 Filter Filter
concept: