943,923 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 15209
  • JSP RSS
Oct 20th, 2008
0

Re: JSPs and Servlets

Expand Post »
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 Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>JSP Page</title>
  5. </head>
  6. <body>
  7. <form method="post" action="DownloadFile">
  8. <table>
  9. <tr>
  10. <td colspan="2">
  11. <input type="submit" value="Download">
  12. </td>
  13. </tr>
  14. </table>
  15. </form>
  16. </body>
  17. </html>


code of servlet i am calling on button click

Java Syntax (Toggle Plain Text)
  1. /*
  2.  * DownloadFile.java
  3.  *
  4.  * Created on October 20, 2008, 5:46 PM
  5.  */
  6.  
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12.  
  13. /**
  14.  *
  15.  * @author 72737
  16.  * @version
  17.  */
  18. public class DownloadFile extends HttpServlet {
  19.  
  20. private String original_filename = "MYFILE.txt";
  21.  
  22. private String filename="D:\\ABC.txt";
  23.  
  24. /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  25.   * @param request servlet request
  26.   * @param response servlet response
  27.   */
  28. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  29. throws ServletException, IOException {
  30.  
  31. File f = new File(filename);
  32. int length = 0;
  33. ServletOutputStream op = response.getOutputStream();
  34. ServletContext context = getServletConfig().getServletContext();
  35. String mimetype = context.getMimeType( filename );
  36.  
  37. //
  38. // Set the response and go!
  39. //
  40. //
  41. response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
  42. response.setContentLength( (int)f.length() );
  43. response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
  44.  
  45.  
  46. //
  47. // Stream to the requester.
  48. //
  49. byte[] bbuf = new byte[filename.length()];
  50. DataInputStream in = new DataInputStream(new FileInputStream(f));
  51.  
  52. while ((in != null) && ((length = in.read(bbuf)) != -1))
  53. {
  54. op.write(bbuf,0,length);
  55. }
  56.  
  57. in.close();
  58. op.flush();
  59. op.close();
  60.  
  61.  
  62.  
  63. }
  64.  
  65. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  66. /** Handles the HTTP <code>GET</code> method.
  67.   * @param request servlet request
  68.   * @param response servlet response
  69.   */
  70. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  71. throws ServletException, IOException {
  72. processRequest(request, response);
  73. }
  74.  
  75. /** Handles the HTTP <code>POST</code> method.
  76.   * @param request servlet request
  77.   * @param response servlet response
  78.   */
  79. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  80. throws ServletException, IOException {
  81. processRequest(request, response);
  82. }
  83.  
  84. /** Returns a short description of the servlet.
  85.   */
  86. public String getServletInfo() {
  87. return "Short description";
  88. }
  89. // </editor-fold>
  90. }



Thanks
Last edited by peter_budo; Oct 20th, 2008 at 4:31 pm. Reason: Keep It Organized - please use [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
narayanabhilash is offline Offline
6 posts
since Dec 2007
Oct 22nd, 2008
0

Re: JSP download file

> i want to trace these button events.

You can't trap this event.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Mar 26th, 2011
0

Downloading

Some error while downloading..java.io.FileNotFoundException: c:\ABC.txt (The system cannot find the file specified.)
Last edited by vidyashanmugam; Mar 26th, 2011 at 4:38 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vidyashanmugam is offline Offline
2 posts
since Mar 2011
Apr 6th, 2011
-1
Re: JSP download file
hi these code is not working it throws file not found Exception
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kajendran t is offline Offline
1 posts
since Apr 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: closing pop up
Next Thread in JSP Forum Timeline: Invalidating Session





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC