1

I am trying to download excel file from server after clicking on a link. I have written the below JSP and Servlet code. JSP and Servlet are both in the same folder. I get the error "The requested resource (/BulkAccess/Download) is not available" after clicking on hyperlink to download the excel file.

JSP Page

  <body>

  <a href="Download"> Sample Excel File </a>

  </body>

Servlet

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

@SuppressWarnings("serial")
public class Download extends HttpServlet {
    public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {

        String filename = "C:\\excelFile.xls";


        ServletOutputStream out = response.getOutputStream();
        FileInputStream in = new FileInputStream(filename);

        response.setContentType("application/vnd.ms-excel");
        response.addHeader("content-disposition",
                "attachment; filename=" + filename);

        int octet;
        while((octet = in.read()) != -1)
            out.write(octet);

        in.close();
        out.close();
    }

    public void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}

I have the mapping in web.xml. below is the code.

<servlet>
<servlet-name>Download</servlet-name>
<servlet-class>com.abc.bulk.Download</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Download</servlet-name>
<url-pattern>/Download</url-pattern>
</servlet-mapping>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
naveen kumar
  • 41
  • 1
  • 6
  • 10

1 Answers1

0

It seems that you have problem in calling Servlet.

This should work.

 <a href="<%=request.getContextPath() %>/Download"> Sample Excel File </a>   

Also, You have mentioned

JSP and Servlet are both in the same folder

Generally, JSP goes under /webapp or /WebContent and Servlets goes under /src/mypackage.

Unrelated to the question: Its a good practice to use lowercase URL mapping.

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96