Dear All,
In my Application i am downloading a file. on button click using servlet.
after clicking the button servlet called and download dialog box opens with open / save and cancel button.

i want to trace these button events.
on open or save i want to update status 'true' in to database and on cancel button i want to update status false for agian download.


to download a file code is given below ..


Code for index page

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
<form method="post" action="DownloadFile">
    <table>
        <tr>
            <td colspan="2">
                <input type="submit" value="Download"> 
            </td>
        </tr>
    </table>
</form>
    </body>
</html>

code of servlet i am calling on button click

/*
 * DownloadFile.java
 *
 * Created on October 20, 2008, 5:46 PM
 */

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author 72737
 * @version
 */
public class DownloadFile extends HttpServlet {

    private String original_filename = "MYFILE.txt";

    private String filename="D:\\ABC.txt";
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> 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 );
    
            //
            //  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 + "\"" );
    
            
            //
            //  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);
            }
    
            in.close();
            op.flush();
            op.close();
    
    
    
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> 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 <code>POST</code> 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";
    }
    // </editor-fold>
}

Thanks

Recommended Answers

All 4 Replies

> i want to trace these button events.

You can't trap this event.

Some error while downloading..java.io.FileNotFoundException: c:\ABC.txt (The system cannot find the file specified.)

hi these code is not working it throws file not found Exception

<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username" />
</div>

<div>
<label for="pass">Password (8 characters minimum):</label>
<input type="password" id="pass" name="password" minlength="8" required />
</div>

<input type="submit" value="Sign in" />

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.