Invoking Other Web Resources

Web components can bring up other Web resources in two ways:-

  • Indirect.
  • Direct.

Indirect Method

In the indirect method web component can be  invokes another Web resource when it embeds in content returned to a client a URL that points to another Web component. In the Bookstore (this examples share common classes and a database schema) application, most Web components contain embedded URLs that point to other Web components. In the indirect method servlet will return the resultant HTML to the browser which will point to another Servlet or web resources.

Eg:- ShowCartServlet indirectly invokes the CatalogServlet through the embedded URL /bookstore1/catalog.

Direct Method

In the direct method a Web component can also directly invoke another resource while it is executing. In the direct method Web resource (Servlet/JSP) from Servlet program itself, by using an object of RequestDispatcher.There are two possibilities:

1)      It can include the content of another resource.

2)       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.

We 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.

1) Including Other Resources in the Response

It is often useful to include another Web resource, for example, banner content or copyright information, in the response returned from a Web component. To include another resource, invoke the include method of a RequestDispatcher object:

Syntax:-include (request, response);

If the resource is static, the include method enables programmatic server-side includes. If the resource is a Web component, the effect of the method is to send the request to the included Web component, execute the Web component, and then include the result of the execution in the response from the containing servlet. An included Web component has access to the request object, but it is limited in what it can do with the response object:

  • It can write to the body of the response and commit a response.
  • It cannot set headers or call any method (for example, setCookie) that affects the headers of the response.

The banner for the Bookstore application is generated by BannerServlet. Note that both the doGet and doPost methods are implemented because BannerServlet can be dispatched from either method in a calling servlet.

Eg: –

public class BannerServlet extends HttpServlet {
public void doGet (HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<body bgcolor=\"#ffffff\">" +
"<center>" + "<hr> <br> &nbsp;" + "<h1>" +
"<font size=\"+3\" color=\"#CC0066\">Duke's </font>" +
<img src=\"" + request.getContextPath() +
"/duke.books.gif\">" +
"<font size=\"+3\" color=\"black\">Bookstore</font>" +
"</h1>" + "</center>" + "<br> &nbsp; <hr> <br> ");
}
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<body bgcolor=\"#ffffff\">" +
"<center>" + "<hr> <br> &nbsp;" + "<h1>" +
"<font size=\"+3\" color=\"#CC0066\">Duke's </font>" +
<img src=\"" + request.getContextPath() +
"/duke.books.gif\">" +
"<font size=\"+3\" color=\"black\">Bookstore</font>" +
"</h1>" + "</center>" + "<br> &nbsp; <hr> <br> ");
}
}

2) Transferring Control  to Another Web Component

In some applications, we might want to have one Web component do preliminary processing of a request and have another component generate the response. For example, we 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, we 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, we 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 we have already accessed a ServletOutputStream or PrintWriter object within the servlet, we cannot use this method; it throws an IllegalStateException.