Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

setCharacterEncoding in HttpServletRequest doesn't seem to work

843842Nov 11 2009 — edited Nov 11 2009
Hello people

I make the following request on the client side
String addr = "blabla:8080/servlet?" +  URLEncoder.encode(request, "UTF-8");
URL urlAddr = new URL(addr);
urlAddr.openConnection();
So that a post request is sent to the servlet. lets say my request is ....?request=getUsers&username=bla, which is sent encoded
On the other side I call
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
System.out.println(req.getParameter("request"));
}
prints null. and when I call req.getQueryString() I still get the encoded string. Thats of coruse becase the request contains %3D instead of '='. On the java documentation it says about setCharacterEncoding:
This method must be called prior to reading request parameters or reading input using getReader().
.
Now My question is, what is wrong with this logic?. Of course one can call getQueryString then apply URLDecoder.decode and then parse the string into a Map, but I guess there should be a smarter way to do it

Many thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 9 2009
Added on Nov 11 2009
1 comment
822 views