TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Servlet Chaining using request dispatcher
Satyapriya Nayak
Sep 30, 2019
14
k
0
2
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
This is a process to make available request of user to multiply servlets.
Introduction
This is a process to make available requests of users to multiply servlets. The user provides request to the first servlet present in the chain. The first servlet uses the form fields present in the request. The first servlet can transfer the request to another servlet with the execution control. The second servlet can use the request, as it is available to itself. This process can be repeated for any number of servlets. The last servlet present in the chain provides a response to the user. All servlet present before the last servlet remains invisible to the user.
Including Response
This is a process to provide a response to the user by including the response of other pages into the response of the first servlet. The user provides the request of the first servlet, the first servlet can transfer the request with execution control to another servlet or page. The second servlet or page can use the request available from the first servlet. The second servlet provides response and execution control to the first servlet. The first servlet provides response to the user by including the response of the second page or servlet. This process can be repeated for any number of servlets. The user fills like getting the response of only one page.
Steps to follow for forward and include
Create an object of javax.servlet.servletContext by using getServletContext() method of generic servlet.
Create an object of javax.servlet.requestDispather by using getRequestDispatcher() of servlet context.This method accepts name and path of the forward and include file.
Use forward () method of RequestDispatcher to forward the request or use include () of RequestDispatcher to include the response.
Ex:-
<
html
>
<
head
>
<
title
>
Login
<
/title
>
</
head
>
<
body
bgcolor
=
"orange"
>
<
br
>
<
p
>
<
h3
>
<
center
>
Please enter your user name and password
<
/center
>
</
h3
>
<
/p
>
<
br
>
<
br
>
<
form
action
="./valid
"
method
="
post "
>
<
center
>
Username
</
center
>
<
center
>
<
input
type
="
text "
name
="
t1 "
>
</
center
>
<
center
>
Password
</
center
>
<
center
>
<
input
type
="
password "
name
="
p1 "
>
</
center
>
<
center
>
<
input
type
="
submit "
name
="
Submit "
value
="
Submit "
>
</
center
>
</
form
>
</
body
>
</
html
>
valid.java file
import
javax.servlet.*;
import
javax.servlet.http.*;
import
java.io.*;
import
java.sql.*;
public
class
valid
extends
HttpServlet {
public
void
doGet(HttpServletRequest req, HttpServletResponse res)
throws
IOException, ServletException {
String s1 = req.getParameter(
"t1"
);
String s2 = req.getParameter(
"p1"
);
try
{
Class.forName(
"sun.jdbc.odbc.JdbcOdbcDriver"
);
Connection con = DriverManager.getConnection(
"jdbc:odbc:dsn1"
,
"system"
,
"pintu"
);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"select * from userinfo where name='"
+ s1 +
"' and pass='"
+ s2 +
"'"
);
ServletContext sc = getServletContext();
RequestDispatcher rd1 = sc.getRequestDispatcher(
"/inbox"
);
RequestDispatcher rd2 = sc.getRequestDispatcher(
"/error"
);
if
(rs.next())
rd1.forward(req, res);
else
rd2.forward(req, res);
}
catch
(Exception e) {
System.out.println(e);
}
}
}
inbox.java file
import
javax.servlet.http.*;
import
javax.servlet.*;
import
java.io.*;
public
class
inbox
extends
HttpServlet {
public
void
doGet(HttpServletRequest req, HttpServletResponse res)
throws
ServletException, IOException {
String str = req.getParameter(
"t1"
);
PrintWriter out = res.getWriter();
out.println(
"<html><body>"
);
out.println(
"<h1>Welcome:: "
+ str +
"</h1>"
);
out.println(
"</body></html>"
);
}
}
error.java file
import
javax.servlet.http.*;
import
javax.servlet.*;
import
java.io.*;
public
class
error
extends
HttpServlet {
public
void
doGet(HttpServletRequest req, HttpServletResponse res)
throws
ServletException, IOException {
PrintWriter out = res.getWriter();
out.println(
"<html><body>"
);
out.println(
"<h1 align='center'>Invalid uid and password</h1>"
);
ServletContext sc = getServletContext();
RequestDispatcher rd1 = sc.getRequestDispatcher(
"/login.html"
);
rd1.include(req, res);
out.println(
"</body></html>"
);
}
}
Servlet Chaining using request dispatcher
Next Recommended Reading
Strategy Design Pattern using Java Sample