Step6: Servlet collaboration and communication + code

1- Request Dispatcher Interface-
Definition-RequestDispatcher is used to dispatch request to any resources on the server.
Syntax- RequestDispatcher rs = request.getRequestDispatcher(“resource name”);

– Methods of RequestDispatcher
(i)-forward method-
Syntax-void forward(ServletRequest request,ServletResponse response)

-This method forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.

Example 2- Web app to create a servlet which sends request object to another servlet using forward method.

Output-ezgif-2-e98f54cdd381

Project- 3 files-

1-Xy.java (inside package ‘abc’)
2-Nm.java(inside package ‘abc’)
3-index.html(inside WebContent folder)

1.Xy.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet("/Xy")
public class Xy extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd=request.getRequestDispatcher("Nm");
rd.forward(request, response);
}

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

}

2.Nm.java(servlet)


package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet("/Nm")
public class Nm extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.print("i am Nm servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

3.index.html

<html>
<body>
<a href=”Xy”>click here</a>
</body>
</html>

2-Include method-
Syntax- void include(ServletRequest request, ServletResponse response)

-This method includes the content of a resource (servlet, JSP page, HTML file) in the response.

Example 3- Web app to create a servlet which includes data of html page into a servlet using include method.

Output-ezgif-2-a3020e4a4f7c

Project 2 files-

1-Yz.java (inside package ‘ab’)
2-index.html(inside WebContent folder)

1-Yz.java


package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet("/Yz")
public class Yz extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out =response.getWriter();
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request, response);
out.print("this is from Yz servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

2-index.html

<html>
<body>
<a href=”Yz”>click here</a>
</body>
</html>

2-SendRedirect-
This method redirects the response to another resource.
Syntax– response.sendRedirect(“resource name”);

Example 4- Web app to create a servlet which executes another servlet using sendRedirect method.

Output-ezgif-2-2b5a5f21ef1e

Project 3 files-

1-Qw.java (inside package ‘ab’)
2-Zx.java(inside package ‘ab’)
3-index.html(inside WebContent folder)

1.Qw.java(servlet)


package ab;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet("/Qw")
public class Qw extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("Zx");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

2-Zx.java(servlet)


package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet("/Zx)
public class Zx extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("this is Zx servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

3-index.html

<html>
<body>
<a href=”Qw”>click here</a>
</body>
</html>

 

#Difference between sendRedirect method and methods of Request Dispatcher interface(forward and include)#

1-sendRedirect() makes the client(browser) create a new request to get to the resource, the user can see the new URL

2-RequestDispatcher get the resource in same request and URL does not changes.

3-sendRedirect() works on response object while RequestDispatcher works on request object.