Monday, 20 February 2006

Head First deployment

One aspect of studying for the SCWCD exam with Head First is how it subtly drills webapp deployment into your head.

While it might be tempting to just start automating it in an IDE, like NetBeans, the command line compiling, directory layout, Tomcat pushes, restarts, etc. have the added benefit of getting the reader to understand how it all is suppose to work.

I love it. Have I mentioned that I'm a geek?

Posted by caffeinated at 10:59 PM in SCWCD

When to set response headers...

The simple answer is: ...always before you flush your response to the client.

In studying for the SCWCD exam I was left with the impression that if you attempted to play with the response after it was committed, a java.lang.IllegalStateException would be thrown.

No! Do not let this trip you up on the exam.

For instance, below is some code I came across that had me saying "Whoa! this is going to throw an IllegalStateException. Danger Will Robinson!"

  ...
  ServletOutputStream out = response.getOutputStream();
  ...
  out.flush();
  out.close();

  // set headers
  response.setContentType("image/png");
  response.setHeader("Cache-Control","no-cache");
  response.setHeader("Pragma","no-cache");
}

So as it turns out nothing was happening. I thought that the ISE was being skirted because headers are buffered and the default buffer size was just large enough...but it was a poster on JavaRanch that clued me in.

The Servlet specification answers the question quite clearly: headers set after the response is committed (javax.servlet.ServletResponse#isCommitted()) are simply ignored. (see SRV 5.2)

Posted by caffeinated at 7:14 PM in SCWCD
« February »
SunMonTueWedThuFriSat
   1234
567891011
12131415161718
19202122232425
262728