1 / 24

Objectives In this lesson you will learn about: Inter-servlet communication

Objectives In this lesson you will learn about: Inter-servlet communication Single-threaded and multi-threaded servlets Using servlet filters to modify the request and response objects. Inter-Servlet Communication

nara
Download Presentation

Objectives In this lesson you will learn about: Inter-servlet communication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objectives • In this lesson you will learn about: • Inter-servlet communication • Single-threaded and multi-threaded servlets • Using servlet filters to modify the request and response objects

  2. Inter-Servlet Communication • In a Web application, various components of the application, such as servlets might need to communicate with each other to process client requests. • A servlet in a Web application can forward a request to another servlet if any exception occurs while processing the request. • The inter-servlet communication techniques can be used in the following two ways: • Using the request dispatcher object • Using the servlet request object

  3. Inter-Servlet Communication (Contd.) • A RequestDispatcher object: • Is an object of the javax.servlet.RequestDispatcherinterface that allows inter-servlet communication. • Is used to include contents of another servlet. • Is used to forward request to another servlet.

  4. Inter-Servlet Communication (Contd.) • To share data between servlets, you can use the request object in your servlets that need to communicate. • The setAttribute()method of the javax.servlet.ServletRequest interface can be used to set values of data in the request object. • Other servlets of the application can use the getAttribute()method of the javax.servlet.ServletRequest interface to retrieve the value of the data.

  5. Demonstration-Calling an Error Page Using the RequestDispatcher Object • Problem Statement • InfoSuper Corp. has created a dynamic Web site. Whenever an error occurs, the stack trace displayed in the browser is difficult to understand. David Wong, the system analyst of the company, asks Don Allen, the software programmer of the company, to create a customized error page. Whenever an exception is thrown by a servlet, it should dispatch the request to the customized error page using the RequestDispatcher object. Then, the error page should display the error in the browser in a more understandable format. Don decides to create an application and test it by passing default string value, samplestring, and converting the same into an integer value, which throws a NumberFormatException exception.

  6. Demonstration-Calling an Error Page Using the RequestDispatcher Object (Contd.) • Solution • To solve the preceding problem, Don needs to perform the following tasks: • Create a Web applictaion. • Create a servlet that forwards the request object to an error page. • Create a servlet that acts as a custom error page to display the error message. • Set the welcome page for the Web application. • Build and run the Web application.

  7. Servlet Threading Model • The servlet specification defines the following two threading models: • Multi-threading model • Single-threading model

  8. Servlet Threading Model (Contd.) • While developing servlets in a multi-threaded model, threading issues needs to be handled to protect the shared resource. • To develop thread-safe servlets, first identify the types of attributes that are inherently thread-safe and the types that need to be guarded for thread safety.

  9. Servlet Threading Model (Contd.) • The various thread-safe capabilities of attributes, methods, and fields in a servlet are: • init() and destroy() methods • Local variables • Request attributes • Context attributes • Data stored in session attributes • The two approaches that can be followed for developing thread-safe servlets are: • Synchronizing block of codes for accessing a shared resource • Synchronizing methods for accessing a shared resource

  10. Servlet Filters • Servlet filters: • Are objects that intercept the requests and response that flow between a client and a servlet. • Modify the headers and content of a request coming from a Web client and forward it to the target servlet. • Intercept and manipulate the headers and contents of the response that the servlet sends back.

  11. Servlet Filters (Contd.) • The advantages of using servlet filters are: • You can identify the type of request coming from the Web client, such as HTTP and File Transfer Protocol (FTP) and invoke the servlet that needs to process the request. • You can validate a client using servlet filters before the client accesses the servlet. • You can retrieve the user information from the request parameters to authenticate the user. • You can use servlet filters to identify the information about the MIME types and other header contents of the request. You can then use the filter to transform the MIME types into compatible types corresponding to the servlet.

  12. Servlet Filters (Contd.) • You can use servlet filters to facilitate a servlet to communicate with the external resources. • You can use servlet filters to intercept responses and compress it before sending the response to the client.

  13. Servlet Filters (Contd.) • Programming of filters include: • Creating filters • Deploying servlet filters

  14. Servlet Filters (Contd.) • The following code snippet shows a filter which calculates the time taken by a servlet to process the client request: • public class ProcessingTimeFilter implements Filter { • FilterConfig flt_cnfg = null; • public void init(FilterConfig f_cnfg) throws ServletException { • this.flt_cnfg = f_cnfg; } • public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletExcept { •   long service_Start = System.currentTimeMillis(); • chain.doFilter(request, response); • long service_Stop = System.currentTimeMillis(); • long serviceTime = (service_Stop - service_Start);

  15. Servlet Filters (Contd.) • String path = ((HttpServletRequest)request).getRequestURI(); • flt_cnfg.getServletContext().log("Time taken to process request for: " +path+" is: "+serviceTime+ " milliseconds"); } • public void destroy () • { • this.flt_cnfg = null; • } • }

  16. Servlet Filters (Contd.) • To deploy a filter: • Add the filter to the Web application • Map the filter to the Web application

  17. Servlet Filters (Contd.) • To add filter to the Web application, you need to perform the following steps in the web.xml file through NetBeans IDE: • Select Filters tab in the web.xml file. The following window is displayed:

  18. Servlet Filters (Contd.) • Click the Add Filter Element button. The Add Servlet Filter dialog box appears, as shown in the following figure: • Type ProcessingTimeFilter in the Filter Name text box. • Click the Browse button. The Browse Files dialog box appears. • Select the ProcessingTimeFilter.java by expanding the Source Packages node.

  19. Servlet Filters (Contd.) • Click the Select Files button. The focus returns to the Add Servlet Filter dialog box. • Click the OK button. The following window is displayed:

  20. Servlet Filters (Contd.) • The steps to map the filter to the selected application are: • Select Filters tab in the web.xml file. • Expand the Filter Mappings node.

  21. Servlet Filters (Contd.) • Click the Add button. The Add Filter Mapping dialog box appears, as shown in the following figure:

  22. Servlet Filters (Contd.) 4. Type /* in the URL Pattern text box. 5. Click the OK button.

  23. Summary • In this lesson, you learned: • You can forward requests to other servlets using the forward()method of the RequestDispatcher object. • You can include the content of other servlets in a servlet using the include()method of the RequestDispatcher object. • You can perform inter-servlet communication using the RequestDispatcher and servlet request object.

  24. Summary (Contd.) • The multi-threaded servlet model is the default threading model of a servlet. • Various functions of a servlet filter are: • Identifying the type of client browser. • Identifying the character encoding used by the client browser. • You can develop servlet filters using Filter interface, FilterConfig interface, ServletRequest interface, and ServletResponse interface. • You can add filter to a Web application and map it to a servlet to filter the contents of a servlet.

More Related