java4all@1986 java. Powered by Blogger.

Example on sendRedirect Mechanism?

>> Tuesday, June 7, 2011

When we want that someone else should handle the response of our servlet, then there we should use send Redirect() method.
In send Redirect whenever the client makes any request it goes to the container, there the container decides whether the concerned servlet can handle the request or not.
If not then the servlet decides that the request can be handle by other servlet or jsp.
Then the servlet calls the send Redirect() method of the response object and sends back the response to the browser along with the status code.
Then the browser sees the status code and look for that servlet which can now handle the request.
Again the browser makes a new request, but with the name of that servlet which can now handle the request and the result will be displayed to you by the browser.
In all this process the client is unaware of the processing.
In this example we are going to make one html in which we will submit the user name and his password.
The controller will check if the password entered by the user is correct or not.
If the password entered by the user is correct then the servlet will redirect the request to the other servlet which will handle the request.
If the password entered by the user is wrong then the request will be forwarded to the html form.

HTML file:
<em><html>

<head>
<title>New Page 1</title>
</head>

<body>

<form method="POST" action="/SendRedirect/SendRedirectServlet">
<p>Enter your name��������
<input type="text" name="username" size="20"></p>
<p>Enter your password� <input type="text" name="password"
size="20"></p>
<p>����������
�� ���������
����������
��
<input type="submit" value="Submit" name="B1"></p>
</form>

</body>

</html>.</em>
SendRediectservlet Class
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SendRedirectServlet extends HttpServlet{
  protected void doPost(HttpServletRequest request, HttpServletResponse
    response)throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  String name = request.getParameter("username");
  String password = request.getParameter("password");
  if(name.equals("James")&& password.equals("abc")){
  response.sendRedirect("/SendRedirect/ValidUserServlet");
  }
  else{
  pw.println("u r not a valid user");
  }
  }
} 
ValidUserServlet class 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ValidUserServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse
  response)throws ServletException, IOException {
  PrintWriter pw = response.getWriter();
  pw.println("Welcome to roseindia.net " + " ");
  pw.println("how are you");
}
}
WEB.XML FILE:

 
 Zulfiqar
 SendRedirectServlet
 
 
 Zulfiqar
 /SendRedirectServlet
 
 
 Hello
 ValidUserServlet
 
 
 Hello
 /ValidUserServlet
 

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP