This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Response Redirection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Response Redirection" Watch "Response Redirection" New topic
Author

Response Redirection

Austin Hoffman
Greenhorn

Joined: Feb 07, 2005
Posts: 21
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

Joined: Dec 11, 2004
Posts: 13410

Is it writing "Admin Login" or is it redirecting?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Austin Hoffman
Greenhorn

Joined: Feb 07, 2005
Posts: 21
It's simply redirecting me to the JSP.

Regards,
Austin
Austin Hoffman
Greenhorn

Joined: Feb 07, 2005
Posts: 21
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

Joined: Jan 22, 2004
Posts: 185
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

Joined: Feb 07, 2005
Posts: 21
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

Joined: Dec 15, 2003
Posts: 57
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

Joined: Jan 22, 2004
Posts: 185
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

Joined: Feb 07, 2005
Posts: 21
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Response Redirection
 
Similar Threads
how to redirect from https to http
IllegalStateException: Cannot forward after response has been committed
Redirect in Struts
Redirect one url to another.
sendRedirect curios behavior