SriN (1) [Avatar] Offline
#1
Hello,
I've tried running the listing 4.8 which is:

public void service(HttpServletRequest req, HttpServletResponse res)
throws javax.servlet.ServletException, java.io.IOException {
res.setContentType("application/jar");
OutputStream os = res.getOutputStream();
//1K buffer
byte[] bytearray = new byte[1024];
ServletContext context = getServletContext();
URL url = context.getResource("/files/test.jar");
InputStream is = url.openStream();
int bytesread = 0;
while( (bytesread = is.read(bytearray) ) != -1 )
{
os.write(bytearray, 0, bytesread);
}
os.flush();
is.close();
}

I've created a directory 'files' in the mail directory of my web application and placed a 'test.jar' in that directory. However, when I run this servlet, it gives NullPointerException.

Could someone please help me with how to successfully run this.

Many thanks in advance.

SriN