INTER-SERVLET COMMUNICATION
Now, we shall study why we need InterServlet communication. Servlets which are
running together in the same server have several ways to communicate with each
other. There are three major reasons to use interservlet communication:
running together in the same server have several ways to communicate with each
other. There are three major reasons to use interservlet communication:
a) Direct servlet manipulation / handling
A servlet can gain access to the other currently loaded servlets and perform some task
on each. The servlet could, for example, periodically ask every servlet to write its state
to disk to protect against server crashes.
on each. The servlet could, for example, periodically ask every servlet to write its state
to disk to protect against server crashes.
Direct servlet manipulation / handling involves one servlet accessing the loaded
servlets on its server and optionally performing some task on one or more of them. A
servlet obtains information about other servlets through the ServletContext object.
Use getServlet() to get a particular servlet:
public Servlet ServletContext.getServlet(String name) throws ServletException
servlets on its server and optionally performing some task on one or more of them. A
servlet obtains information about other servlets through the ServletContext object.
Use getServlet() to get a particular servlet:
public Servlet ServletContext.getServlet(String name) throws ServletException
This method returns the servlet of the given name, or null if the servlet is not found.
The specified name can be the servlet’s registered name (such as “file”) or its class
name (such as “com.sun.server.webserver.FileServlet”). The server maintains one
servlet instance per name, so getServlet(“file”) returns a different servlet instance than
getServlet(“com.sun.server.webserver .FileServlet”).
You can also get all of the servlets using getServlets():
public Enumeration ServletContext.getServlets()
The specified name can be the servlet’s registered name (such as “file”) or its class
name (such as “com.sun.server.webserver.FileServlet”). The server maintains one
servlet instance per name, so getServlet(“file”) returns a different servlet instance than
getServlet(“com.sun.server.webserver .FileServlet”).
You can also get all of the servlets using getServlets():
public Enumeration ServletContext.getServlets()
This method returns an Enumeration of the servlet objects loaded in the current
ServletContext. Generally there’s one servlet context per server, but for security or
convenience, a server may decide to partition its servlets into separate contexts. The
enumeration always includes the calling servlet itself.
Let us take an example to understand how we can view the currently loaded servlets.
//Example Checking out the currently loaded servlets
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Loaded extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.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());
out.println();
}
}
}In the above example, it retrieves its ServletContext to access the other servlets loaded
in the server. Then it calls the context’s getServletNames() method. This returns an
enumeration of String objects that the servlet iterates over in a while loop. For each
name, it retrieves the corresponding servlet object with a call to the context’s
getServlet() method. Then it prints three items of information about the servlet: its
name, its class name, and its getServletInfo() text.
b) Servlet reuse
Another use for interservlet communication is to allow one servlet to reuse the
abilities (the public methods) of another servlet. The major challenge with servlet
reuse is for the “user” servlet to obtain the proper instance of “usee” servlet when the
usee servlet has not yet been loaded into the server. For example a servlet named as
ChatServlet was written as a server for chat applets, but it could be reused
(unchanged) by another servlet that needed to support an HTML-based chat interface.
Servlet can be done with the user servlet to ask the server to load the usee servlet, then
call getServlet() to get a reference to it. Unfortunately, the Servlet API distinctly lacks
any methods whereby a servlet can control the servlet life cycle, for itself or for other
servlets. This is considered a security risk and is officially “left for future
consideration.” Fortunately, there’s a backdoor we can use today. A servlet can open
an HTTP connection to the server in which it’s running, ask for the unloaded servlet,
and effectively force the server to load the servlet to handle the request. Then a call to
getServlet() gets the proper instance
c) Servlet collaboration
Sometimes servlets have to cooperate, usually by sharing some information. We call
this type of communication as servlet collaboration. Collaborating servlets can pass
the shared information directly from one servlet to another through method
invocations. This approach requires each servlet to know the other servlets with which
it is collaborating. The most common situation involves two or more servlets sharing
state information. For example, a set of servlets managing an online store could share
the store’s product inventory count. Session tracking can be considered as a special
case of servlet collaboration
Colloboration using system property list:
One simple way for servlets to share information is by using Java’s system-wide
Properties list, found in the java.lang.System class. This Properties list holds the
standard system properties, such as java.version and path. separator, but it can also
hold application-specific properties. Servlets can use the properties list to hold the
information they need to share. A servlet can add (or change) a property by calling:
System.getProperties().put(“key”, “value”);
That servlet, or another servlet running in the same JVM, can later get the value of the
property by calling:
String value = System.getProperty(“key”);
The property can be removed by calling:
System.getProperties().remove(“key”);
No comments:
Post a Comment