The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

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