Showing posts with label web resources. Show all posts
Showing posts with label web resources. Show all posts

Thursday, 15 December 2011

Accessing the Web Context


The context in which Web components execute is an object that implements the ServletContext interface. You retrieve the Web context with the getServletContext method. The Web context provides methods for accessing:
  • Initialization parameters
  • Resources associated with the Web context
  • Object-valued attributes
  • Logging capabilities
The Web context is used by the Duke's Bookstore filters filters.HitCounterFilter and OrderFilter, discussed in the section Filtering Requests and Responses. The filters store a counter as a context attribute. Recall from Controlling Concurrent Access to Shared Resources that the counter's access methods are synchronized to prevent incompatible operations by servlets that are running concurrently. A filter retrieves the counter object with the context's getAttribute method. The incremented value of the counter is recorded with the context's log method.
public final class HitCounterFilter implements Filter {
   private FilterConfig filterConfig = null;
   public void doFilter(ServletRequest request,
      ServletResponse response, FilterChain chain) 
      throws IOException, ServletException {
      ...
      StringWriter sw = new StringWriter();
      PrintWriter writer = new PrintWriter(sw);
      ServletContext context = filterConfig.
         getServletContext();
      Counter counter = (Counter)context.
         getAttribute("hitCounter");
      ...
      writer.println("The number of hits is: " +
         counter.incCounter());
      ...
      context.log(sw.getBuffer().toString());
      ...
   }
}

Invoking Other Web Resources


Web components can invoke other Web resources in two ways: indirect and direct. A Web component indirectly invokes another Web resource when it embeds in content returned to a client a URL that points to another Web component. In the Duke's Bookstore application, most Web components contain embedded URLs that point to other Web components. For example,ShowCartServlet indirectly invokes the CatalogServlet through the embedded URL /bookstore1/catalog.
A Web component can also directly invoke another resource while it is executing. There are two possibilities: it can include the content of another resource, or it can forward a request to another resource.
To invoke a resource available on the server that is running a Web component, you must first obtain a RequestDispatcher object using the getRequestDispatcher("URL") method.
You can get a RequestDispatcher object from either a request or the Web context; however, the two methods have slightly different behavior. The method takes the path to the requested resource as an argument. A request can take a relative path (that is, one that does not begin with a /), but the Web context requires an absolute path. If the resource is not available, or if the server has not implemented a RequestDispatcher object for that type of resource, getRequestDispatcher will return null. Your servlet should be prepared to deal with this condition.
Read more on Web Resource 

Transferring Control to Another Web Component


In some applications, you might want to have one Web component do preliminary processing of a request and have another component generate the response. For example, you might want to partially process a request and then transfer to another component depending on the nature of the request.
To transfer control to another Web component, you invoke the forward method of a RequestDispatcher. When a request is forwarded, the request URL is set to the path of the forwarded page. If the original URL is required for any processing, you can save it as a request attribute. The Dispatcher servlet, used by a version of the Duke's Bookstore application described in the section A Template Tag Library, saves the path information from the original URL, retrieves a RequestDispatcher from the request, and then forwards to the JSP page template.jsp.
public class Dispatcher extends HttpServlet {
   public void doGet(HttpServletRequest request, 
      HttpServletResponse response) {
      request.setAttribute("selectedScreen",
         request.getServletPath());
      RequestDispatcher dispatcher = request.
         getRequestDispatcher("/template.jsp");
      if (dispatcher != null)
         dispatcher.forward(request, response);
   }
   public void doPost(HttpServletRequest request, 
   ...
}
 
The forward method should be used to give another resource responsibility for replying to the user. If you have already accessed a ServletOutputStream or PrintWriter object within the servlet, you cannot use this method; it throws an IllegalStateException.