In Focus
Kolkata Chapter Developer Day: May 25
C# Corner Delhi Chapter Meet, May-18, 2013 Official Recap
Istanbul Chapter May 2013 2nd Seminar Announced : Learn WinJS, Windows Store, MVC
ANNOUNCEMENT: April 2013 Month Winners
Email :
Password :
Remember me?
Forgot password
Contribute
An Article
A Blog
A News
A Video
A Link
An Interview Question
Ask a Question
TECHNOLOGIES
.NET 4.5
Current Affairs
Learn .NET
SharePoint 2010
Web Development
.NET Assemblies
Databases & DBA
LightSwitch 2012
SharePoint 2013
Web Services in C#
ADO.NET in C#
Design & Architecture
LINQ
Silverlight with C#
Windows 8
AJAX in C#
Exception Handling C#
Mobile & Embedded
Smart Devices
Windows Azure
Android Programming
Graphics Design
Office 2013
SQL
Windows Controls C#
Articles C#
Hardware
OOP/OOD
SQL Server 2012
Windows Forms C#
ASP.NET Controls in C#
How do I
Operating Systems
Testing
Windows Phone 8
ASP.NET MVC with C#
HTML 5
PHP
TypeScript
Windows Server 2012
ASP.NET Programming
Internet & Web
Products
Visual C#
Windows Store Apps
C# Language
iPhone/iPad
Project Management
Visual Studio .NET
Workflow Foundation in C#
C# Tutorials
Java
Reports using C#
Visual Studio 2010
WPF
C, C++, MFC
Java and .NET
Robotics & Hardware
Visual Studio 2012
XAML
Career Advice
JavaScript, CSS
Security in .NET
WCF with C#
XML
Chapters
JQuery
Request a new Category
|
View All
ANSWERS
BLOGS
VIDEOS
INTERVIEWS
BOOKS
LINKS
NEWS
CHAPTERS
CAREER ADVICE
Making your Web Browser in C#
HTML 5 vs Native Apps
Secure Your Android Application from Hackers
Why to fear Object Oriented Programming approach
Check upcoming B'day in Asp.net
Class vs Structure
Happy Birthday Destin Joy
Compare strings using StringComparison.Ordin ...
Access Master Page Control In Child Page
Popup Example in WPF
Blog
Servlet Chaining using request dispatcher
Posted by
Satyapriya Nayak
in
Blogs
|
Java
on
Sep 28, 2011
This is a process to make available request of user to multiply servlets.
Tweet
3888
0
0
This is a process to make available request of user to multiply servlets. 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 sevlets. The last servlet present in the chain provides response to the user. All servlet present before the last servlet remains invisible to the user.
Including Response
This is a process to provide response to the user by including response of other pages into the response of first servlet. The user provides request of 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 response of the second page or servlet. This process can be repeated for any number of servlet. The user fills like getting 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>");
}
}
This Feature is Sponsored By
DynamicPDF Merger is a developers dream for interacting with any existing PDF documents. Merge, append, split, form fill, flatten stamp and so much more.
Threading in java
Comparing equality of Strings in Java
post comment
COMMENT USING
PREMIUM SPONSORS
DynamicPDF Developer Components
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
More Blogs from this Blogger
Validate TextBox using JavaScript
Display XML File Data into GridView using DataTable
Search Record Display in GridView
Display Data from Database to Label
Delete multiple row by selecting checkbox on Data Gridview
Update Male TO Female and Female To Male with single query
Display username belong to selected item value from combo box
Related records from one textbox to another
Fetch multiple items to combobox from db
Uncheck CheckBoxList items when button is clicked
View All
SPONSORED BY
DynamicPDF ReportWriter Suite
PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Follow @twitterapi
Latest Blogs
Android Studio not Starting Up After Installation
Check upcoming B'day in Asp.net
Get The Value From DataGridView To TextBox On c#
How to set Date Range and Restrict Date in jQuery?
Get Meta Data Of DLL File In Visula Studio
What is jQuery holdReady ?
Making your Web Browser in C#
Compare strings using StringComparison.OrdinalIgnoreCase
How to write or create a text file in C#
Binding Text of One controls with another in WPF
View All
Poll
Result
All Polls
What is the best product to build Mobile apps?
What is the best product to build Mobile apps
PhoneGap
Icenium
Xamarin Studio
KendoUI Mobile
JQueryMobile
Native iOS/Android
What is the best product to build Mobile apps
Options
Votes
%
PhoneGap
0
0.00
Icenium
0
0.00
Xamarin Studio
0
0.00
KendoUI Mobile
0
0.00
JQueryMobile
1
100.00
Native iOS/Android
0
0.00
Total
1
100%