Servlet Chaining pages using RequestDispatcher


            A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.

The pathname specified may be relative, although it cannot access outside the current application. 
This method returns null if the servlet container cannot return a RequestDispatcher.

Forward
            Forwards a request from a servlet to another resource (servlet or JSP file) on the server. 
            We can use one servlet to do preliminary processing of a request and another resource to generate the output response.
            Any response from the first servlet will be automatically cleared before forwarding to second servlet.
            The forwared Servlet will be using the same request and response object send by first servlet.

Include
            Includes the content of a resource (servlet or JSP page) in the response. In essence, this method enables programmatic server-side includes.
            The included servlet cannot change the response status code or set headers of first servlet, any attempt to make a change is ignored.

Example
Html code
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Candidjava RequestDispatcher Forward example <br> <br>

<form action="TestForwardController" method="post">

Enter username :<input type="text" name="username"> <br>
Enter password :<input type="password" name="password"><br>
<input type="submit" value="Login">


</form>

<br> <br><br><br>
Candidjava RequestDispatcher Include example <br> <br>
<form action="TestIncludeController" method="post">

Enter username :<input type="text" name="username"> <br>
Enter password :<input type="password" name="password"><br>
<input type="submit" value="Login">


</form>

</body>
</html>
Servlet Include
package com.candidjava;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestIncludeController
 */
public class TestIncludeController extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String un=request.getParameter("username");
		String pw=request.getParameter("password");
		
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.print(" <b> Welcome to TestIncludeController(Servlet1) <br>");
		out.print("username received is "+un + " </b> " );
		
		out.print("<br>");
		out.print("<br>");
		out.print("<br>");
		
		RequestDispatcher dis= request.getRequestDispatcher("/TestController");
		dis.include(request, response);
	}

}

Servlet Forward
package com.candidjava;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestForwardController
 */
public class TestForwardController extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String un=request.getParameter("username");
		String pw=request.getParameter("password");
		
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.print(" <b> Welcome to TestForwardController(Servlet1) <br>");
		out.print("username received is "+un + " </b> " );
		
		out.print("<br>");
		out.print("<br>");
		out.print("<br>");
		
		RequestDispatcher dis= request.getRequestDispatcher("/TestController");
		dis.forward(request, response);
		
	}

}
TestController
package com.candidjava;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class TestController
 */
public class TestController extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String un=request.getParameter("username");
		
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.print(" <b> Welcome to TestController(Servlet2) <br>");
		out.print("username received is "+un + " </b> " );
	}

}

Screenshot
                        
            
                        

Download
RequestDispatcher example war
RequestDispatcher example zip



Related Post

Comments


©candidjava.com