what is servlet collaboration?

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) + ".");
      }
   
   }
}

How to get client machine name and Ip address from Httpservlet?

In HttpServletRequest there are two methods
 a. getRemoteAddr()
 b. getRemoteHost()
 which will provide the necessary information.
 Actually in request header following information is stored.


Ip address of client m/c = request.getRemoteAddr();

Hostname of client =  request.getRemoteHost()

What is difference between include action and include directive in JSP?



Before starting the difference  discussion,  first, we have to understand the JSP life cycle. when a request first comes to a JSP it will translate into servlet then compile and load into web container for further use. Subsequent requests  will serve by this translated servlet.


Let say we have a JSP page called greet.jsp which includes another JSP call  header.jsp. Now it can be included in two ways
 1. by page directive 
 2. by JSP action tag
1. By page directive : syntax is <%@ include file="header.jsp" %>
now when greet.jsp page translated  into a  servlet all the code written in the header.jsp goes to translated servlet's service()(incase of Httpservlet it's //doget() or dopost()) method. so it acts as one servlet which contains two JSP's code ( header.jsp + greet.jsp  ) and ready for serve subsequent requests. Now in mean time suppose  developer change the content of header.jsp, this change will not reflect to translated servlet as it is already compiled and loaded in the web container  . To make this happen you have to restart web container then it will recompile  but some web container smart enough to pick up this changes. so  include page directive is  not a good option for dynamic pages.


2. By JSP action tag: syntax

<jsp:include page="header.jsp">
 Here inclusion is done at request time so it maintains two servlets unlike page directive so it will show updated value each time.  so action tag is the good choice  for dynamic pages.