Đang chuẩn bị liên kết để tải về tài liệu:
Mastering Jakarta Struts phần 2

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Độc giả, tuy nhiên, nên liên hệ với các công ty thích hợp cho biết thêm thông tin về thương hiệu và đăng ký. Bản quyền © 2002 bởi Wiley Publishing, Inc Tất cả các quyền. Được đăng bởi Wiley Publishing, Inc, Indianapolis, Indiana đã công bố đồng thời tại Canada. Không có phần nào của ấn phẩm này có thể được sao chép | The ServletContext HTML HEAD TITLE Context TITLE HEAD BODY Try to get the USERNAME attribute from the ServletContext String userName String application.getAttribute USERNAME If there was no attribute USERNAME then create one and add it to the ServletContext if userName null Don t try to add it just say that you can t find it out.println b Attribute USERNAME not found else out.println b The current User is userName b BODY HTML Note In the Contextjsp we are using two JSP implicit objects the application object which references the ServletContext and the out object which references an output stream to the client. We will discuss each of these later in this chapter. Now copy Context.jsp to the CN7XL7W_HƠMỄ webapps wileyapp directory restart Tomcat and open your browser first to the following URL http localhost 8080 wileyapp Context.jsp You should see a page similar to Figure 2.4. Figure 2.4 The output of the Context.jsp prior to the execution of the servlet ContextServlet. 21 Using Servlets to Retrieve HTTP Data You should notice that the Contextjsp cannot find a reference to the attribute USERNAME. It will not be able to find this reference until the reference is placed there by the ContextServlet. To do this open your browser to the following URL http localhost 8080 wileyapp servlet chapter2.ContextServlet You should see output similar to Figure 2.5. Figure 2.5 The output of the ContextServlet. After running this servlet the wileyapp Web application has an object bound to the name USERNAME stored in its ServletContext. To see how this affects another Web component in the wileyapp Web application open the previous URL that references the Contextjsp and look at the change in output. The JSP can now find the USERNAME and it prints this value to the response. Note To remove an object from the ServletContext you can restart the JSP servlet container or use the ServletContext.removeAttribute method. Using Servlets to Retrieve HTTP Data In this our final section on servlets