Servlet to download a file returning errors
Hello All,
I wrote following servlet to download a file:
__________________________________________
package controller;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.util.*;
class DownloadServ extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
response.setContentType("application/pdf");
ServletContext ctx=getServletContext();
InputStream is=ctx.getResourceAsStream("/
21stCenturyTiger_Christmas_wishes_2009.pdf");
int read=0;
byte[ ] bytes=new byte[1024];
OutputStream os=response.getOutputStream();
while((read=is.read(bytes)) != -1){
os.write(bytes,0,read);
}
os.flush();
os.close();
}
}
_________________________________________________________
But it is showing the following error in browser:
________________________________________________________
exception
javax.servlet.ServletException: Error instantiating servlet class
com.example.web.DownloadServ
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:
104)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
261)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:
852)
org.apache.coyote.http11.Http11AprProtocol
$Http11ConnectionHandler.process(Http11AprProtocol.java:584)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:
1508)
java.lang.Thread.run(Unknown Source)
________________________________________________________
Please help me with correcting the code.
Thanks in Advance.
Regards
Kuber