Peter wrote:
>
> Is it possible for a servlet to write a file onto the clients hard drive ?
>
> I'm making a small payroll program where the user wants to save the payslip
> onto his/her hard drive so that it can be printed later.
>
> So far I've only managed to get the servlet to print files back to the
> server under a directory owned by user/group nobody.
>
> Somebody help...............

Servlets are executed on server, not on the client machine, so they
cannot write to disks on the client machine. But you can pop-up
browser's "Save as ..." dialog with this code:

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

public class SaveAs extends HttpServlet
{
 public void doGet (HttpServletRequest request,
                     HttpServletResponse response)
                   throws ServletException, IOException
 {
   PrintWriter out;
   if(request.getHeader("user-agent").indexOf("MSIE")!= -1)
   {
   response.setContentType("application/x-my-type");
   response.setHeader("Content-Disposition","inline;filename=my_file_name");
   }
   else
   {
   response.setContentType("application/octet-stream");
   response.setHeader("Content-Disposition","inline;filename=my_file_name.txt");
   }
   out = response.getWriter();
   try {
   /
   out.println("This text will be saved in a text file.");
   //-----
   } catch (Exception ex) {out.println(ex.getMessage());ex.printStackTrace(out);}
 }

}


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   INET, a.s.                          Mgr. Martin Kuba
Kralovopolska 139                  e-mail: [EMAIL PROTECTED]
  601 12 Brno                      WWW: http://www.inet.cz/~makub/
 Czech Republic                    tel: +420-5-41242414/33
--------------------------------------------------------------------
PGP fingerprint = D8 57 47 E5 36 D2 C1 A1  C3 48 B2 59 00 58 42 27
 http://wwwkeys.cz.pgp.net:11371/pks/lookup?op=index&search=makub
--------------------------------------------------------------------

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to