This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

issue with res.sendRedirect  RSS feed

 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

In my doPost method I'm doing some thing in a try catch block.If some exception occurs i want it to redirect to some error page,using response.sendRedirect(some url).Before entering the try -catch block i'm checking for session,if session is null using the response.sendRedirect i'm redirecting to another page.Here it works.But in catch block it is not working(Plz check my code snipplet).Can some one tell me why my servlet behaves like this.


public void doPost(HttpServletRequest req, HttpServletResponse res) throws Servlet
{
boolean issizeok = true;
PrintWriter out = res.getWriter();
res.setHeader("Pragma","no-cache");
res.setHeader("Cache-Control","no-cache");
res.addHeader("Vary", "*");

if(httpSession == null)
{
res.sendRedirect(some url);//here this works
return;
}
try
{ //some code
}
catch(Exception e)
{
res.sendRedirect(some url);//Not working
}
}

Thanks in Advance
Chandrasekhar S.
SCJP
 
unni krishna
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Idealy It should work at both places. Just try using RequestDispatcher.forward() method instead.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Not working" is a useless description of what is happening. What does "Not working" mean?
 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

"Not Working" means its not serving the purpose.

In other words the res.sendRedirect(url) not redirecting to the new URL.

Thanks and Regards
Chandrasekhar S
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first step in diagnosis would be to be sure that the redirect statement is even executed. Put in a System.out.println or log statement. You should also search your server logs.
You may well be getting an IllegalStateException thrown by sendRedirect if the response has already written something out (since redirect involves a header, you can't redirect after the headers have been sent.)
Bill
 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William for that reply.

But i'm not getting any IllegalStateException thrown by sendRedirect.The scenario i'll explain.

I want to upload image files to server and i'm using the package from oreilly.

They got one constructor like
MultipartRequest multipartrequest = new com.oreilly.servlet.MultipartRequest (req, tmpDir,LoginInit.UPLOAD_MAX_SIZE);

Here the UPLOAD_MAX_SIZE is set as 1 MB.I want to redirect to an error page when user tries to upload images of size larger than 1 MB.

The exception caught while uploading the image of larger size is
IOException Error: Posted content length of 1297539 exceeds limit of 1048576.Instead of redirecting to the error page,its showing "Cannot find server".

The application server i'm using is jakarta-tomcat-3.3a(Older version).

Can someone shed some light into this.

Thanks & regards
Chandrasekhar
[ July 18, 2004: Message edited by: Chandrasekhar S ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!