Thursday, November 24, 2005

... spilleth over!

Today's earlier rant is spilling over to this post. It has been, what, perhaps a year since I last ranted about work in this blog (it's not meant to be a rant page...) But I've been really busy lately (both at work and troubled somewhat in the heart), and this latest thing just irks me to no end, not to mention it sucked a great many hours!

OK, so for you folks out there who by weird chance might just end up reading this post because Google archive it or something, here are some things to take note when using J2ME client to do a HTTP POST to a servlet sitting on a Tomcat container trying to read a parameter using getParameter(...)

Things which a lot of sample codes probably don't tell you upfront:

1. Order of calls is important
setRequestMethod(HttpConnection.POST), setRequestProperty("Content-Type", "application/x-www-form-urlencoded"), and all setRequestProperty(...) calls must be made before calling getOutputStream()!

2. Sometimes calling outputstream.flush() may result in chunking that some application servers cannot handle. Check yours. Use outputstream.close() to be safe.

3. setRequestProperty("Content-Type", "application/x-www-form-urlencoded") MUST be called.

4. If you don't have setRequestProperty("Content-Length", N) , the servlet side sometimes sees 2 requests-- header & body, rather than just 1 request. (Hint: watch HTTP traffic on your server side)

5. Lastly, of course, don't be silly, remember what you write to outputstream must be in the form of this regular expression: [yourParameterName=yourParameterValue]+ [&nextParameterName=nextParameterValue]*

And don't forget to pray to our Lady, Maria Knotenlöserin

No comments: