On 7/7/09 19:09, siom...@portosdobrasil.gov.br wrote:
Dear all,

I need to log some information only after a user downloads or opens a file.

I am using a servlet for that and the download part works fine.

However I need to identify which button was clicked because in case the user
clicks [CANCEL] I am not supposed to register any information.

I put lots of messages on the code to understand how it works and even if I
click [CANCEL] the messages will be printed showing that all commands will
be executed no matter which button was clicked.

There is nothing in either the Servlet Spec or standard accessible browser functionality that will provide something so specific.

AFAIK the only thing you can look out for is an exception during the phase where you're writing to the output, as interrupting the streaming of data to the output stream may throw a catchable exception.

If the user clicks cancel before then, I doubt whether anything is registered at the server.

Try wrapping that section and catching all exceptions, to see if you can detect a problem. The download will have to be sufficiently large for you to have time to cancel it after it's started.

Also: unless you really mean to use a DataInputStream, you're probably using the wrong class; javadoc: "A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way."

I think you'll also need to examine how you're writing to the output stream, as using a buffer based on the length of the filename is at least one thing wrong with that.

p


Can someone help me to identify which button was clicked?

Thanks

Siomara

===================
package servlets.comum;

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


/**
* Definition of class DownloadFile.
*/

public class DownloadFile extends HttpServlet

{

     private String original_filename = "MYFILE.txt";

     private String filename="C:\\ABC.txt";

     /**
      * Processes requests for both HTTP>GET and POST methods.
      * @param request servlet request
      * @param response servlet response
      */

     protected void processRequest(HttpServletRequest request,
HttpServletResponse response)

     throws ServletException, IOException

     {

         File                f        = new File(filename);

         int                 length   = 0;

         ServletOutputStream op       = response.getOutputStream();

         ServletContext      context  =
getServletConfig().getServletContext();

         String              mimetype = context.getMimeType( filename );

System.out.println("here 1");

         //  Set the response and go!

         response.setContentType( (mimetype != null) ? mimetype :
"application/octet-stream" );

         response.setContentLength( (int)f.length() );

         response.setHeader( "Content-Disposition", "attachment; filename=\""
+ original_filename + "\"" );

System.out.println("here 2");

         //  Stream to the requester.

         byte[] bbuf = new byte[filename.length()];

         DataInputStream in = new DataInputStream(new FileInputStream(f));

         while ((in != null)&&  ((length = in.read(bbuf)) != -1))

         {
             op.write(bbuf,0,length);
         }

System.out.println("here 3");

         in.close();

System.out.println("here 4");

         op.flush();

System.out.println("here 5");

         op.close();

System.out.println("here 6");

     }

     /**
      * Handles the HTTP GET method.
      * @param request servlet request
      * @param response servlet response
      */

     protected void doGet(HttpServletRequest request, HttpServletResponse
response)

     throws ServletException, IOException {

         processRequest(request, response);

     }

     /**
      * Handles the HTTP POST method.
      * @param request servlet request
      * @param response servlet response
      */

     protected void doPost(HttpServletRequest request, HttpServletResponse
response)

     throws ServletException, IOException {

         processRequest(request, response);

     }

     /**
      * Returns a short description of the servlet.
      */

     public String getServletInfo() {

         return "Short description";

     }

}

==============================
Results:

[Tue Jul 07 10:39:43 BRT 2009]  info: postgres: Opened a new connection
[Tue Jul 07 10:39:43 BRT 2009]  info: postgres: Delivered connection from
pool
select arquivolicitacao.licitacaoid, arquivolicitacao.arquivoid,
arquivo.tipoarquivoid, arquivo.tituloapresentacao, arquivo.nomeinterno,
arquivo.localizacaofisica, arquivo.datapublicacaodou, tipoarquivo.descricao
from arquivolicitacao, arquivo, tipoarquivo where
arquivolicitacao.licitacaoid = 5 and arquivolicitacao.arquivoid =
arquivo.arquivoid and arquivo.tipoarquivoid = tipoarquivo.tipoarquivoid
order by datapublicacaodou desc
here 1
here 2
here 3
here 4
here 5
here 6



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to