• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

issue with res.sendRedirect

 
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
 
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.
 
Marshal
Posts: 67340
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
 
Author and all-around good cowpoke
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 ]
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!