1. Newly added event model: By setting Listener, you can do something before and after Servlet initialization. This is the application of the event model in Servlet.
After accessing/bookstore1/enter, ContextListener constructs a DB and places it in the ServletContext. In this way, BookstoreServlet can obtain the initialized DB from the Context.
The core code is as follows:
Context = event. getServletContext ();
.........................................
Context. setAttribute ("bookDB", bookDB );
The implementation of the ContextListener class is very simple. There are only two methods: contextInitialized and contextDestroyed.
Lifetime event:
Not only can ServletContext events be monitored, but also Session events can be monitored, for example, HttpSessionListener and ServletRequestListener.
2 RequestDispatch:
Use RequestDispatch in BookstoreServlet. The code is as follows:
RequestDispatcher dispatcher =
GetServletContext (). getRequestDispatcher ("/banner ");
If (dispatcher! = Null)
Dispatcher. include (request, response );
View web. xml. We can see that/banner corresponds to the BannerServlet class.
A RequestDispatch is a common Servlet class. generally, its application is as follows: a small Servlet class generates partial Response results, and another Servlet contains the results to construct a Response. this small Servlet class can be understood as a webpage template. program Component. in this way, the modularization of Servlet is improved.
Read the source code of the first BookstoreServlet.
3. There are two connections on the page. One is the book information and the other is Start Shopping.
1) click the book information BookDetailsServlet
Retrieve the BookDB from the Context, search for records based on Parameter, list records, and list the two options: add to shopping cart and continue shopping.
1. Add CatalogServlet to the shopping cart to set the shopping cart and list information about checking the shopping cart, books, and books.
2. Check the cartservlet in the shopping cart according to the request value. Nothing New
3. This is the case with other servlets.
2. This should be done for HttpServlet.
1. Get output stream from response
2. Fill in the resp header information
3 Write the body to the output
4. Filter
Concept:
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