Wednesday, December 31, 2014

Inter servlet communication in Java

Major reasons to use inter servlet communication
  • Direct Servlet manipulation/handling: One servlet can access to other loaded servlets on the server and perform some task. One servlet obtains information about other servlets through ServletContext object.
  • getServlet() method returns the servlet of given name
    • public Servlet ServletContext.getServlet(String name) throws ServletException
  • Specified name can be servlet's registered name or class name
  • getServlet("file") returns different instance than getServlet("com.sun.server.webserver.FileServlet") because server maintains one servlet instance per name
  • getServlets() method return enumeration of all servlet objects loaded in current ServletContext.
  • Generally there are only one ServletContext object but for security or other reasons there can be many

Example

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class Loaded extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException{
    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 
    ServletContext context = getServletContext();
    Enumeration names = context.getServletNames();
    while (names.hasMoreElements()){
      String name=(String)names.nextElement();
      Servlet servlet =context.getServlet(name);
      out.println("Servlet name: "+name);
      out.println("Servlet class: "+ servlet.getClass().getName());
      out.println("Servlet info: "+ servlet.getServletInfo());
}}}

Servlet reuse

One servlet can reuse the abilities (public methods) of another servlet. There is challenge though for using methods of unloaded servlets.

Servlet collaboration

Collaboration is done through information sharing. Collaborating servlets can share data directly through method invocations. Servlets must know each other for collaboration. Collaboration can be done through following two methods

Collaboration using system property list

Java's system wide properties list found in java.lang.System class. This list holds standard system properties like java.version, path.separator and other application specific properties. The properties can be accessed by other servlets running on the same JVM. Property class is string based ie key and values are strings.
  • System.getProperties().put("key","value") is called to add or change a property
  • String value=System.getProperty("key") is used to access the value of key
  • System.getProperties().remove("key") is used to remove the property

Collaboration through a shared object

Shared objects can hold pool of shared information and make it available to other servlets when needed. System properties list is special case example of shared object. The shared object often incorporates business logic to manipulate the data. The business logic also protects data by defining mutators, accessors and other methods. Garbage collector might reclaim the shared object when it is not referenced by a loaded servlet. To keep garbage collector at bay, every servlet using a shared object should save reference to the object.

No comments:

Post a Comment