Inter Servlet Communication

It means communication between servlets of a web application

Inter-servlet communication using Request Dispatcher

Request Dispatcher:An object of the javax.servlet.RequestDispatcher interface that allows inter-servlet communication.

Object is used to include ot forward the content of another servlet.

To get RequestDispatcher Object:

//for relative path 
ServletContext.getRequestDispatcher(String resource) 
//for context path
ServletRequest.getRequestDispatcher(String resource) 

RequestDispatcher interface provides two methods:

public void forward(ServletRequest req,ServletResponse res)throws ServletException,java.io.IOException:

Forwards a request from a servlet to another resource on the server.

public void include(ServletRequest req,ServletResponse res)throws ServletException,java.io.IOException:

Includes the content of a resource in the response.

Examples:

RequestDispatcher rd; 
rd = request.getRequestDispatcher("abc.jsp?user=ank");
rd.include(request, response); 

Or

RequestDispatcher dispatcher = 
                 req.getRequestDispatcher("/index.html");
dispatcher.forward(req, res); 

Inter-servlet communication using Send Redirect

Send Redirect can be used to communicate between two servlet present in different servers, the output will be same as request dispatcher forward example but the url of the page will be changed to redirected page

Example:

response.sendRedirect
		("http://localhost:8080/Test/ServletTwo");

Inter-servlet communication example

In this example, we are validating the password entered by the user. If password is correct, it will forward the request to the HomeServlet, otherwise will forward request to login page with an error message: Either username or password is wrong.

In this example, we have created following files:

  • index.html file: for getting input from the user.
  • Login.java file: a servlet class for processing the response. If password is servet, it will forward the request to the welcome servlet.
  • HomeServlet.java file: a servlet class for displaying the Home Page.
  • web.xml file: a deployment descriptor file that contains the information about the servlet.

index.html

<form action="servlet1" method="post"\> 
Name:<input type="text" name="userName"/><br/>  
Password:<input type="password" name="userPass"><br/>  
<input type="submit" value="login"/>  
</form>

login.java

import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
  
  
public class Login extends HttpServlet {  
  
public void doPost(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {  
  
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  
          
    String name=request.getParameter("userName");  
    String pass=request.getParameter("userPass");  
          
    if(pass.equals("servlet"){  
        RequestDispatcher dispatcher=request.getRequestDispatcher("servlet2");  
        dispatcher.forward(request, response);  
    }  
    else{  
        out.print("Either UserName or Password is Wrong!");  
        RequestDispatcher dispatcher=request.getRequestDispatcher("/index.html");  
        dispatcher.include(request, response);  
                      
        }  
    }  
  
}  

HomeServlet.java

import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
  
public class HomeServlet extends HttpServlet {  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {  
  
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  
          
    String name=request.getParameter("userName");  
    out.print("HomePage "+name);  
    }  
  
}  

web.xml

<web-app>  
 <servlet>  
    <servlet-name>Login</servlet-name>  
    <servlet-class>Login</servlet-class>  
  </servlet>  
  <servlet>  
    <servlet-name>HomeServlet</servlet-name>  
    <servlet-class>HomeServlet</servlet-class>  
  </servlet>  
  
  
  <servlet-mapping>  
    <servlet-name>Login</servlet-name>  
    <url-pattern>/servlet1</url-pattern>  
  </servlet-mapping>  
  <servlet-mapping\>  
    <servlet-name>HomeServlet</servlet-name>  
    <url-pattern>/servlet2</url-pattern>  
  </servlet-mapping>  
  
  <welcome-file-list>  
   <welcome-file>index.html</welcome-file>  
  </welcome-file-list>  
</web-app>