Servlet Collaboration

Servlet Collaboration

Servlet Collaboration means how one Servlet communicate another Servlet and pass some information or data from one Servlet to another Servlet.

We can achieve Servlet Collaboration by two way:

  1. Using RequestDispatcher Interface
  2. Using sendRedirect() Method

RequestDispatcher Interface:

1.RequestDispatcher is an Interface that exist on javax.servlet package.

2.We can get Object or instance of RequestDispatcher using below method with the HttpServletRequest object:

RequestDispatcher rd=request.getRequestDispatcher("/pagename");

3.In RequestDispatcher Interface there is no new request is created to call another page.

4.RequestDispatcher Interface is used to call page with in the same project.

5.RequestDispatcher Interface has two methods:

  • include(request,response)
  • forward(request,response)

6.include method of RequestDispatcher Interface is used to add current page response to requested page.

7.forward method of RequestDispatcher Interface is not able to add current page response to requested page.

8.RequestScope, SessionScope and ApplicationScope is applicable for RequestDispatcher interface.

9.Processing is done at Server Side.

For Example:
servlet collaboration

FirstPage.java

package com.deepsingh44.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/first")
public class FirstPage extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req, resp);
	}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		doProcess(req, resp);
	}

private void doProcess(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String name = req.getParameter("name");
		
	if (name.isEmpty()) {
		resp.getWriter().print("Please enter name");
		req.getRequestDispatcher("index.html").include(req, resp);
	} else {
		req.setAttribute("name", name);
		req.getRequestDispatcher("second").forward(req, resp);
	}

	}

}
  

SecondPage.java

package com.deepsingh44.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/second")
public class SecondPage extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		doProcess(req, resp);
}

private void doProcess(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		
		String name = req.getAttribute("name").toString();
		
		resp.getWriter().print("Welcome, " + name);
		
}

}

index.html

  <form action="first">
	<p>
		Enter Name:<input type="text" name="name">
	</p>
	<p>
		<input type="submit" value="submit">
	</p>
</form>
  

Output Screens:


servlet

deepsingh44

servlet

sendRedirect() Method:

1.sendRedirect() is a method of HttpServletResponse object.

2.In sendRedirect() method there is always new request created.

3.In sendRedirect() method we can call internal page and external page as well.

4.In sendRedirect() method requestScope is not applicable.

5.In sendRedirect() method only SessionScope and AppplicationScope is applicable.

6.Processing is done at Client Side.

For Example:
servlet

FirstPage.java

package com.deepsingh44.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/first")
public class FirstPage extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		doProcess(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
		doProcess(req, resp);
}

private void doProcess(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		resp.setContentType("text/html");
		String name = req.getParameter("name");
		
		
		if (name.isEmpty()) {
			resp.getWriter().print("Please enter name");
			req.getRequestDispatcher("index.html").
            include(req, resp);
		} else {
			req.getServletContext().setAttribute("name",name);
			resp.sendRedirect("second");
		}

	}

}
  

SecondPage.java

package com.deepsingh44.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/second")
public class SecondPage extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		doProcess(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
		doProcess(req, resp);
}

private void doProcess(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		
		String name = req.getServletContext().
        getAttribute("name").toString();
		
		resp.getWriter().print("Welcome, " + name);
		
	}

}
  

index.html

  <form action="first">
	<p>
		Enter Name:<input type="text" name="name">
	</p>
	<p>
		<input type="submit" value="submit">
	</p>
</form>
  

Output Screens:






No comments: