Wrox Programmer Forums
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 10th, 2006, 11:50 AM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Save Dialog Box

To open a save dialog box I use a code below:

RequestDispatcher dispatcher =
            getServletConfig().getServletContext().getRequestD ispatcher(/file_name);
        dispatcher.forward(request, response);

It works fine, but my question is how to catch an exeption if the user click cancel in dialog box?



 
Old February 13th, 2006, 05:23 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There's no exception to be caught if the user clicks cancel, since your web application has already completed the request by forwarding it to your passed file_name. If you really want to catch this case, then you have to put a monitor on the piece serving file_name that will check to see if bytes begin to be downloaded. If they don't, then you should take a note of it and have the parent page ping the server back to check for the note. If the note is found, then the parent page should direct the user to your error page using Javascript. It's probably more work than it's worth :).

Jon Emerson
http://www.zoominfo.com/JonEmerson
 
Old February 13th, 2006, 06:15 AM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks,
Could you propose another approach to this problem? How to open a save dialog box from servlet?
Piece of code would be great in this case.

M.

 
Old February 14th, 2006, 03:26 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There aren't really any other alternatives using standard HTML/JS, since the only way to tell if the user clicked "Save" is by monitoring whether they actually start downloading content, and the only way for the parent browser window to detect this state is by pinging back to the server.

I'm unaware whether there's some browser-specific Javascript APIs for determining the state of download boxes. You can probably do an ActiveX extension on Windows to do it, but that would require XP SP2 users to install it. And I'm not sure what you'd do for Firefox.

Jon Emerson
http://www.jonemerson.net/
 
Old February 15th, 2006, 06:34 AM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your response.
I have another problem with dispatcher
I am trying to use
dispatcher.forward(request, response) from servlet to jsp.
I create dispatcher as
      RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp_1/WP/warning_page.jsp");
dispatcher = getServletConfig().getServletContext().getRequestD ispatcher("/jsp_1/WP/warning_page.jsp");
 dispatcher = getServletConfig().getServletContext().getRequestD ispatcher("/jsp_1/WP/warning_page.jsp");
In every case dispatcher is not null but the error is that the warning_page.jsp is not available.
Where I should put my jsp page?
My servlet is in /jsp_1/com/new3/…/Servlet1


 
Old February 15th, 2006, 06:37 AM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry my mistake
My servlet is in /jsp_1/WEB-INF/classes/com/all/new3/Msite/pd/Servlet1.class

 
Old February 15th, 2006, 03:33 PM
Authorized User
 
Join Date: Jan 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to longjava
Default

Your "warning_page.jsp" should go to the directoy:

%CATALINA_HOME%/webapp/jsp_1/WP

where %CATALINA_HOME% is the root directory of where your Tomcat is installed.


 
Old February 16th, 2006, 12:04 PM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

YES, Thx

 
Old February 17th, 2006, 11:06 AM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

import javax.servlet.http.*;
 import javax.servlet.*;
 import javax.servlet.ServletOutputStream;
 import java.io.*;

 public class FileDownloadServlet extends HttpServlet{

 public void doGet(HttpServletRequest req, HttpServletResponse res)
 throws ServletException, IOException
 {
     String fullName = null;
     ServletOutputStream out = res.getOutputStream();
     fullName = "ALA.txt";

     String pathName = "C:/Ala/"+fullName;
     String contentType = getServletContext().getMimeType(pathName);

     if (contentType != null)
         res.setContentType(contentType);
     else
         res.setContentType("application/octet-stream");

     res.setHeader ("Content-Disposition", "attachment; filename=\"" + fullName + "\"");
     FileInputStream fis = null;

     // Return the file
     try {
         fis = new FileInputStream(pathName);
         byte[] buf = new byte[4 * 1024]; // 4K buffer
         int bytesRead;
         while ((bytesRead = fis.read(buf)) != -1)
             out.write(buf, 0, bytesRead);
     }
     catch (FileNotFoundException e) {
          fis.close();
          out.flush();
          out.close();
         out.println("File not found: " + pathName+ ": " + e.getMessage());
     }
     catch (IOException e) {
         out.println("Problem sending file " + pathName + ": " + e.getMessage());
     }
     finally {
         if (fis != null)
             {
              fis.close();
              out.flush();
              out.close();
              }
     }

 }

 }

I have one question. This servlet opens a save dialog box.
What I have to do to display at the same time the page content for example:
out.prinln("xxx"); or include animate gif when the downloading is runing.
At this moment I add "xxx" to the file.


Thanks


 
Old February 23rd, 2006, 09:10 AM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default


RequestDispatcher dispatcher =
            getServletConfig().getServletContext().getRequestD ispatcher(/file_name);
        dispatcher.forward(request, response);

I have one more problem with this RequestDispatcher. After the download I need to redirect to the page. I use:
response.sendRedirect(url);
but every time i get en exception java.lang.IllegalStateException

Do you know the solution for my problem?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to edit the Places Bar of Save As dialog box akbbhatt Visual Studio 2005 0 August 27th, 2008 04:16 AM
How do I forcefully opne the save dialog box janakiraman ASP.NET 3.5 Basics 1 April 16th, 2008 07:35 AM
Save As Dialog Box prasanta2expert Access VBA 0 June 23rd, 2007 07:35 AM
Save dialog box mcinar Classic ASP Basics 6 January 15th, 2005 12:49 AM
Can Open/save dialog box be eliminated? janice_2k4 VBScript 2 June 14th, 2004 03:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.