Link to home
Start Free TrialLog in
Avatar of zensa
zensa

asked on

Download file from servlet

I am new to servlets. I want to render a word document from a servlet.

There is a JSP page

<HTML>
<BODY>
  <a href="Sample">Download</a>
</BODY>
</HTML>

When user clicks on this link, a servlet is called

public class Sample extends HttpServlet {


    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException   {
        String file ="myWord.doc";
        response.setContentType("text/html");
       
            ServletOutputStream loStr = null;
        loStr = response.getOutputStream();
        loStr.write(file.getBytes());
        loStr.flush();
        loStr.close();
       
    }
}

This servlet has to read a word document from the web server file system and download it on user's machine. But what I have written above only prints "myWord.doc" on the browser and does not download the file.
Please help
ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial