Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSP/Servlet redirect problem...

Status
Not open for further replies.

MCAR

Programmer
Oct 2, 2002
5
BR
Hi, I have a page with a form that sends information to a servlet, then it generates a file to be downloaded when the form submit button is pressed.

It works OK, but when the download finishes, the controls in the page (checkboxes, radio, etc) doesn't work anymore, and I get an "access denied" message from the browser (I have javascript functions to check these controls). And only a ctrl-refresh can bring the page to its original state.

I understand that the initial page loses the focus when I submit the data to the servlet. I've tried to put a RequestDispatcher forward method pointing back to the initial page at the end of the servlet, but it didn't work.

I need help.
 
When the servlet downloads the file to the browser, how is the browser handling it? Does it display the file in the same window (so you have to hit Back to return to the form), does it open a new window and display it, or does it bring up a dialog to save the file? What format is the file in? Is your servlet resetting the content type of the response ( ie: response.setContentType("application/vnd.ms-excel"); )?

If you're not already doing so, try putting a target="something" on the submit button so it opens a new window for the file download. Then you should be able to just close that window and your form is still in the original window ready to go.


 
Thanks idarke, I solved the question making the initial page pointing to another page that calls the servlet using a refresh <meta> tag in the page. Like those download sites that say: &quot;If the download doesn't start, click here.&quot;
I found it cool, but your solution is far more simple. Shame on me to read it just today.
 
Hi all,

I am trying to implement the exact same thing as above. I have a JSP that submits a query & a Servlet that creates a temp CSV file.

My question is, how do I send this back to the return view & if possible, automatically popup a SaveAs Dialog?

Code samples will be greatly appreciated.

Many thanks,

Anthony.
 
Oops I left out the most important part of the question:

response.sendRedirect(location) works as long as the temp file is somewhere under my /context, eg


However, for security reasons I rather generate the temp files in either
a) java.io.tmpdir, (eg C:\temp)
b) javax.servlet.context.tempdir (eg Tomcat's work\MainEngine)

The question I want to ask is how to send back the temp file if it's located outside my /context?

Thanks,

Anthony
 
I build temp files in my servlets context and then download them to the client as an exe file. I determine where the file is located by creating a dummy java temp file and then extracting the path:

static String tempDir = null;

if (tempDir == null)
{
try
{
File temp = File.createTempFile(&quot;temp&quot;,&quot;.tmp&quot;);
tempDir = temp.getParent();
temp.delete();
}
catch (IOException e)
{
System.err.println(e.toString());
}
}

then I download the file to the client:

File exeFile = new File(tempDir + &quot;//temp.exe&quot;);

response.setContentType(&quot;application/exe&quot;);
ServletOutputStream out = response.getOutputStream();

int iChar=0;
FileInputStream oFIS = new FileInputStream(exeFile);
while((iChar =oFIS.read())>=0)
out.write(iChar);
oFIS.close();

Since EXE is not defined on the browser as a file type with a default action, a &quot;Save As&quot; dialog pops up.

Maybe some of this can help you...

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
Excellent! This is exactly what I wanted. Thank you! I was doing:

response.sendRedirect(exeFile);

instead of writing the file contents directly to the response output stream as in your example.

Naturally my redirect causes the browser to make a new request to the server for that file, which a) requires exeFile to be a valid URI *within* the servlet context, and b) creates more security concerns.
 
idark,

Can't u use javax.servlet.RequestDispatcher.forward() for that?

-pete
 
We did it that way because we combine an exe, a powerpoint file and an xml file (generated on the fly in a StringBuffer) into a single downloaded exe file. We basically read the exe and send it, then read the powerpoint file and send it,then send the stringbuffer last, all in the same stream.

How would you handle that with forward()?

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
>> How would you handle that with forward()?

idark, i don't know, that's why i was asking you LOL

I would guess something like this would work:
Code:
RequestDispatcher rd =
    this.getServletConfig()
        .getServletContext()
        .getRequestDispatcher(tempDir + &quot;//temp.exe&quot;);
rd.forward();

and wanted to know if you had tried it and found it was not possible.

-pete
 
sendRedirect() & forward() will both work. Redirect will cause an extra round trip call.

idarke: If you want to send 1 file instead of 3, I imagine you can zip it up on the fly & then call forward().

palbano: Writing directly to ServletOutputStream (thanks idarke, I totally forgot I can do that cuz I always use JSP to generate return view instead of Servlet) has 1 advantage in this case:

The KEY is where you want tempDir to be:
getServletContext() ==> tempDir must be in webapp context
getContext() ==> in any webapp context within container's doc root

idarke's code, as is, won't work with RequestDispatcher since his tempDir will map to a real path (in Windows, by default C:\temp, C:\Documents & Settings\BillyBob\Local Settings\temp, etc) instead of
a) response.sendRedirect(tempDir + &quot;//temp.exe&quot;): remote browsers will try to look at their own C:
b) getContext().getRequestDispatcher(tempDir + &quot;//temp.exe&quot;) or getServletContext().getRequestDispatcher(tempDir + &quot;//temp.exe&quot;) will probably return NULL (I didn't try)

I posted the orig question because I wanted my tempDir OUTSIDE doc root like idarke's, but I can't do it with either redirect() or forward(). By responding via ServletOutputStream, my Servlet can now create temp files anywhere.

Cheers,

Anthony.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top