Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Response Redirection

 
Austin Hoffman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I read in a book that you can't redirect after the response has been flushed, doing so will throw exception.

If this is true then why is this not throwing an IllegalStateException


public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{


response.setContentType("text/html");
PrintWriter pw=response.getWriter();

pw.println("<b>Admin Login </b>"+getInitParameter("adminEMail"));
pw.flush();

response.sendRedirect(request.getContextPath() +"/view.jsp");//BOOM! this is gonna explode

pw.close();

This is working fine and redirection is also taking place. I'm using Tomcat 5

Regards,
Austin
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it writing "Admin Login" or is it redirecting?
 
Austin Hoffman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's simply redirecting me to the JSP.

Regards,
Austin
 
Austin Hoffman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!,
It is very strange, why is the container redirecting me to te JSP view. I have also tested the code with Tomcat 4.

Regards,
Austin
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Austin,

I think it is related to response's buffer.

I read in a book that you can't redirect after the response has been flushed, doing so will throw exception.


In your code, you use
pw.flush();
, which means the message is flushed to the lower level (i.e. response). When this message is sent back to the client depends on response's buffer size.

What you can do is adding another line in your code.
response.setContentType("text/html");
PrintWriter pw=response.getWriter();

pw.println("<b>Admin Login </b>"+getInitParameter("adminEMail"));
pw.flush();
response.flushBuffer();

response.sendRedirect(request.getContextPath() +"/view.jsp");//BOOM! this is gonna explode

Dan
 
Austin Hoffman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit surprising....

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{


response.setContentType("text/html");
PrintWriter pw=response.getWriter();

pw.println("<b>Admin Login </b>"+getInitParameter("adminEMail"));
pw.flush();
response.flushBuffer();



response.sendRedirect(request.getContextPath() +"/view.jsp");//BOOM! this is gonna explode

pw.close();


}
I'm using Tomcat 4 and the oly output is --Admin Login
What is going on here?

Regards,
Austin
 
Ravinder S Edhan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Austin,

I tried this code

package com.flush.example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FlushExample extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String fromHTML = request.getParameter("formVar");
out.println("This is Flush example");
out.flush();

RequestDispatcher view = request.getRequestDispatcher("jsp/test.jsp");
view.forward(request,response);
}
}

It gives the exception on servers as follows:

java.lang.IllegalStateException: Cannot forward a response that is already committed

and thus not forwarding the request to JSP.

I'm using Weblogic server.

Cheers
Ravinder S Edhan
[ April 06, 2005: Message edited by: Ravinder S Edhan ]
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is really wierd. I create a test servlet and deploy it to Tomcat 5.

Here is the code.

public class TestServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//TODO Method stub generated by Lomboz

response.setContentType("text/html");
PrintWriter pw=response.getWriter();

pw.println("<b>Admin Login </b>");
pw.flush();

response.flushBuffer();

pw.println("response has been " + response.isCommitted());



pw.println(request.getContextPath() +"/servlet/HelloWorldExample");
response.sendRedirect(request.getContextPath() +"/servlet/HelloWorldExample");//BOOM! this is gonna explode

pw.close();
}
}

The return message I got is

Admin Login response has been true /servlets-examples/servlet/HelloWorldExample

"true" means response is committed, then an IlleagalStateException must be thrown due to the following.

sendRedirect

public void sendRedirect(java.lang.String location)
throws java.io.IOException

Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Parameters:
location - the redirect location URL
Throws:
java.io.IOException - If an input or output exception occurs
IllegalStateException - If the response was committed or if a partial URL is given and cannot be converted into a valid URL
 
Austin Hoffman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What danny said is correct.....even i have tries with Tomcat 4 and 5.
It's very surprising....once the reponse is committed, you can't redirect...

Someone posted that it is throwing exception using Weblogic, but weblogic also follows the j2ee spec and tomcat isthe official ref. implementation.

Regards,
Austin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic