tags:

views:

21

answers:

0

I have the following codes running in Netbeans6.5 with GlassFish. But in Eclipse3.4.2 with Weblogic, it returns a null pointer exception. The exception points to the first line of the code. What possibly caused the exception in Eclipse with Weblogic?

ServletContext context = (ServletContext) getExternalContext().getContext();
HttpServletResponse response = (HttpServletResponse) getExternalContext().getResponse();
response.setContentType("application/force-download");
String downloadFile = this.getSessionBean1().getDownloadFile();
response.addHeader("Content-Disposition", "attachment; filename=\"" + 
  downloadFile + "\"");
byte[] buf = new byte[1024];
try{
 String realPath = context.getRealPath("/resources/" + downloadFile);
 File file = new File(realPath);
 long length = file.length();
 BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
 ServletOutputStream out = response.getOutputStream();
 response.setContentLength((int)length);
  while ((in != null) && ((length = in.read(buf)) != -1)) {
  out.write(buf, 0, (int)length);
 }
  in.close();
  out.close();
}
catch (Exception exc){
  exc.printStackTrace();
}