• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems redirecting user from servlet

 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I've run into a problem with my servlet that has left me baffled, and I'm hoping some one here would be able to help me out.
My servlet creates a pdf-document from some data in a database. After the document is created, the servlet should redirect the user's browser to this new pdf.
This works fine on my own computer, but when I transferred the files to the server where it should run the redirection stopped working.
The URL into which the servlet redirects is parsed from a known directory structure and the referer address.
The servlet runs on Tomcat 4.0.3, and the form which calls the servlet is on IIS 5.0. Both of these reside on the same Win2K server. (The combination of all these was NOT my idea.)
Here is the line that should do the redirect:

Any help is greatly appreciated.
 
Vin Man
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Not contributing to the solution, but I have a need to do the similar thing - gernerating PDF from db and redirecting to the new pdf. Could you please give me some direction on where to obtain the pdf API's, and the general pattern of doing it. Any pointer to avoid pitfalls would be appreciated... Thanks in advance.
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the Apache project's Fop to create the pdf. You can find that from http://xml.apache.org/fop.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably you've debugged the redirect URL to make sure it's valid?
I read somewhere else here that someone was having problems with IE and pdf files. Have you tried it in Netscape or Mozilla? I think the bug was something to do with specifying the content type as application / pdf. Sorry I can't be more specific.

Adam
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have checked the redirect URL, it works fine if I write it on the browser's location bar.
I have run across some problems with pdf files and IE earlier on, but at that time my servlet wrote the content of the pdf file directly to the browser. Now it writes the data to a file and then redirects the browser to that file. Is it even possible to set the content type of a page you are redirecting the user to?
 
Pritam Banerjee
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is possible to set the content type for the file you would like to show to the user.
Code example as follows:
String mimetype = "application/pdf";
resp.setContentType(mimeType);
RequestDispatcher rd = getServletContext().getRequestDispatcher(fileName);
if(rd != null)
rd.include(req, resp);
Note: You don't redirect to the file, rather you just include the file as part of the current response. That way you are eligible to set the content type.
Also the include() statement should be the last line of your doPost() or doGet(), since you just want to display the file and nothing before or after that.
Hope this helps.
Regards,
Pritam
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks a lot Pritam, but unfortunately that doesn't quite apply to my problem. Even if I did include the file to the response, I still would have to redirect in some cases.
That is because in some cases the redirection doesn't go to the pdf-file, but to an asp-file.
I'm starting to think this could be a problem caused by the IIS configuration.
 
Pritam Banerjee
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you know in which condition you need to redirect to an ASP file then under that condition simply include() without setting the content type.
if(condition to go to ASP){
RequestDispatcher rd = getServletContext().getRequestDispatcher(fileName);
if(rd != null)
rd.include(req, resp);
}
else{
String mimetype = "application/pdf";
resp.setContentType(mimeType);
RequestDispatcher rd = getServletContext().getRequestDispatcher(fileName);
if(rd != null)
rd.include(req, resp);
}

This works. I guarantee you.
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll have to try that. Thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!