Servlet Collaboration means how one servlet can communicate with other. Sometimes servlets are to pass the common information that is to be shared directly by one servlet to another through various invocations of the methods. To perform these operations, each servlet needs to know the other servlet with which it collaborates. Here are several ways to communicate with one another:
1. ServletContext : In an application, there must be one Servlet context which is shared by all the servlets in this application. It works as a global map .each servlet put necessary information , which needs to be shared
and other can get that info
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>com.example.ServletTest</servlet-class>
</servlet>
<context-param>
<param-name>name</param-name>
<param-value>Shamik Mitra</param-value>
</context-param>
To get this
getServletContext().getInitParameter("email");
Request Dispatcher:
request.setAttribute("name", "Shamik Mitra")
request.getRequestDispatcher("destination_name").forward(req,res);
request.getRequestDispatcher("destination_name").include(req,res);
Java Singleton class :
public class Javacontext
{
private static Javacontext ctx = new Javacontext();
private String name ="Shamik";
private Javacontext()
{
}
public static Javacontext getInstance()
{
return ctx;
}
public String getname()
{
return name;
}
}
Java Properties: Using java properties you can share information
import java.util.*;
public class PropDemo {
public static void main(String args[]) {
Properties prop= new Properties();
Set<String> key;
prop.put("name", "Shamik Mitra");
// Show all states and capitals in hashtable.
key= prop.keySet(); // get set-view of keys
Iterator itr = key.iterator();
while(itr.hasNext()) {
str = (String) itr.next();
System.out.println("The key is" +
str + " name is " + prop.getProperty(str) + ".");
}
}
}
1. ServletContext : In an application, there must be one Servlet context which is shared by all the servlets in this application. It works as a global map .each servlet put necessary information , which needs to be shared
and other can get that info
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>com.example.ServletTest</servlet-class>
</servlet>
<context-param>
<param-name>name</param-name>
<param-value>Shamik Mitra</param-value>
</context-param>
To get this
getServletContext().getInitParameter("email");
Request Dispatcher:
request.setAttribute("name", "Shamik Mitra")
request.getRequestDispatcher("destination_name").forward(req,res);
request.getRequestDispatcher("destination_name").include(req,res);
Java Singleton class :
public class Javacontext
{
private static Javacontext ctx = new Javacontext();
private String name ="Shamik";
private Javacontext()
{
}
public static Javacontext getInstance()
{
return ctx;
}
public String getname()
{
return name;
}
}
Java Properties: Using java properties you can share information
import java.util.*;
public class PropDemo {
public static void main(String args[]) {
Properties prop= new Properties();
Set<String> key;
prop.put("name", "Shamik Mitra");
// Show all states and capitals in hashtable.
key= prop.keySet(); // get set-view of keys
Iterator itr = key.iterator();
while(itr.hasNext()) {
str = (String) itr.next();
System.out.println("The key is" +
str + " name is " + prop.getProperty(str) + ".");
}
}
}
Post a Comment