Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

RequestDispatchar in Java, What is RequestDispatchar?

It is used to navigate application content from the web server without using a web browser.

RequestDisptchar Interface Provides better performance as compared to redirect() but we can not manage browser state using session and cookies.URL will not change in Requestdispatcher Interface.


It contain navigation from JSP to Servlet or Servlet to Servlet or Servlet to JSP.


redirect() is also used to navigate from JSP to Servlet and Servlet to Servlet but it completes roundtrip from server to client then the client to another page hence it will manage browser state using session and cookies and URL will change.



Requestdispatcher has two different methods for Page Redirection:-

1) Forward:-

 It is used to forward a request from one servlet to another without changing the URL because it will directly forward application content from Server to Server.

   Syntax
   
    RequestDispatcher req=  request.getRequestDispatcher("Servlet2");
    req.forward(request, response);


URL Will not be changed it will show Servlet1 but it has been forwarded to servlet2

http://localhost:8080/WebConept3/Servlet1



Limitation:-  we can not handle the client-side state management option means session and cookie because it will directly send data from the server end.



package scs;

import java.io.IOException;

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

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname="abcd@gmail.com",upass="12345";
if(uname.equals("abcd@gmail.com")&& upass.equals("12345"))
{
    RequestDispatcher req = request.getRequestDispatcher("Dashboard");
    req.forward(request,response);
    
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}




package scs;

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

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

/**
 * Servlet implementation class Dashboard
 */
@WebServlet("/Dashboard")
public class Dashboard extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Dashboard() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<h2>Welcome in Admin Dashboard</h2>");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}



2)  Include:-

     It is used to merge the request of Servlet2 content into Servlet1 content.

     It is similar to include the tag of JSP.


    http://localhost:8080/WebConept3/Servlet1


URL will not change only content of servlet2 will be merged into servlet1

It will also not use Cookie or Client Side State Management Option.






RequestDisptachar interface will be used for server-side navigation from one Servlet to another Servlet or Servlet to JSP.

Example of Include() in Java:-


package scs;

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

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

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname="abcd@gmail.com",upass="12345";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if(uname.equals("abcd@gmail.com")&& upass.equals("12345"))
{
    RequestDispatcher req = request.getRequestDispatcher("Dashboard");
    out.write("Welcome in Login");
    //req.forward(request,response);
    req.include(request,response);
    
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}


package scs;

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

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

/**
 * Servlet implementation class Dashboard
 */
@WebServlet("/Dashboard")
public class Dashboard extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Dashboard() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<h2>Welcome in Admin Dashboard</h2>");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}




Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post