• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Response Redirection

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic