Latest

recent

Servlet interaction techniques


In our examples so far, we have demonstrated various servlet concepts and techniques. In most cases, these examples consisted of stand-alone servlet programs that handled a request and returned a response.

In the real world, servlets would not be stand-alone programs, but rather, they would be grouped together as part of an application; and the application components could consist of servlets, shared objects, and other resource files, such as HTML and JSPs. 

It would be expected that the servlets of an application would need some way to communicate and interact, either with each other or with the resources of the application. The ServletContext object, which we describe in more detail in “Application level scope” on page 89, provides a way for us to define this Web application level, and the resources that it can access and interact with.
This section describes various techniques for servlet interaction and communication. We will discuss the following types:

❑  Servlet collaboration: Two techniques for servlet collaboration are servlet filtering and chaining.Here multiple servlets collaborate on producing a single response for a client. The servlets themselves are not really interacting directly with each other, rather, the Web application server is
responsible for tying the servlets together.

❑  Calling servlets from servlets: Since servlets are Java programs, they can do anything a standard Java program can do, such as make a network connection. In this way, we have implicit servlet interaction. Additionally, because a servlet is just a Java class, we can instantiate and call a servlet’s public methods.
❑  Response redirection: We can redirect the servlet response to another application resource, such as another servlet or an error page (HTML or JSP). 

❑  Request dispatching: Through the RequestDispatcher object, we can forward a request to another servlet, which can handle the request and return the response. Additionally, we can include directly another servlet’s response within the context of a calling servlet. We can use request dispatching to dispatch the handling to another active application resource.

❑  Resource usage: We can interact with an application’s resources through the servlet context. The ServletContext object allows us access to these resources through the getResource method.

❑  Sharing of objects in scope: There are three levels of object scope for a servlet. Application scope is between all servlets in the same application, and is accessed through the ServletContext object. User session objects are accessed through the HttpSession object, and request level objects through
the servlet request.
Servlet interaction techniques Reviewed by 1000sourcecodes on 21:08 Rating: 5
Powered by Blogger.