.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Request Dispatcher in servlet

Posted By:satyapriyanayak       Posted Date: June 10, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know about Request Dispatcher in servlet.
 

Servlet Chaining using request dispatcher: -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

1.Create an object of javax.servlet.servletContext by using getServletContext() method of generic servlet.

2.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.

3.Use forward () method of RequestDispatcher to forward the request or use include () of RequestDispatcher to include the response.


Example:-

<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>");
  }
}
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend