1- Request Dispatcher Interface-
Definition-RequestDispatcher is used to dispatch request to any resources on the server.
Syntax-

1
RequestDispatcher rs = request.getRequestDispatcher("resource name");

– Methods of RequestDispatcher
(i)-forward method-
Syntax-

1
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-

oD.gif

Project Structure-

we.png

4 files-

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

1.Xy.java(servlet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Xy extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd=request.getRequestDispatcher("second");
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
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.web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="UTF-8"?>
<display-name>ForwrdExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>q</servlet-name>
<servlet-class>ab.Xy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>q</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>w</servlet-name>
<servlet-class>ab.Nm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>w</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>
</web-app>

4.index.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="first">click here</a>
</body>
</html>

[]

2-Include method-
Syntax-

1
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-

asdasd

Project Structure

unti

3 files-

1-Yz.java (inside package ‘ab’)
2-web.xml(inside WEB-INF folder)
3-index.html(inside WebContent folder)

1-Yz.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
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-web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<display-name>IncludExampl</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>a</servlet-name>
<servlet-class>ab.Yz</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/go</url-pattern>
</servlet-mapping>
</web-app>

3-index.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="go">click here</a>
</body>
</html>

2-SendRedirect-
This method redirects the response to another resource.
Syntax-

1
response.sendRedirect("resource name");

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

Project Structure-

snsp

4 files-

1-Qw.java (inside package ‘ab’)
2-Zx.java(inside package ‘ab’)
3-web.xml(inside WEB-INF folder)
4-index.html(inside WebContent folder)

1.Qw.java(servlet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package ab;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class Qw extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("second");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

2-Zx.java(servlet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
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-web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8"?>
<display-name>SendrdrctExampl</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>q</servlet-name>
<servlet-class>ab.Qw</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>q</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>w</servlet-name>
<servlet-class>ab.Zx</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>w</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>
</web-app>

4-index.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="first">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.

Advertisements