Tuesday, March 01, 2005

Unicode (or non ISO-8859-1) encoding and text fields in "mulitpart/form-data" form

Today, I was hunting for a bug where the form enctype="multipart/form-data" (typically used for "uploading" files from browser). I had couple of text fields in this form. With little research I found that the IE always encodes a "multipart/form-data" as ISO-8859-1 even though your pageEncoding may be UTF-8 (or anything else) for that matter.

There is a Struts bug posted here and it has some workarounds (not staright-forward though).

The definite way to make it work is to extend ActionServlet. Click here for a tutorial on extending action servlet.

And the overriding this method:
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
try{
request.setCharacterEncoding("utf-8");
}catch(Exception ex){

}
super.process(request,response);
}

This should do the job

5 comments:

Anonymous said...

hi Venkk,

have similar problem. :(( . but not using struts. just a jsp with mutipart/form-data. and trying to make some validation from a worker bean. And throwing the message to the jsp. while the message popped up, all my japanese characters turned junk...
any solutionss ???

Unknown said...

hi.. i think for plain JSPs fix is pretty simple... I am not sure whether u post to a servlet or a jsp... whichever its at that point put request.setCharacterEncoding("UTF-8").. things shud just work fine..

Anonymous said...

Thanks a lot...we are sending it to a jsp......should I set the encoding in the beginging of the destination JSP..or should I set characterEncoding in the sender JSP..

I tried at both places..but in the destination it is Junked anyway....
..am I not doing it right way??

Unknown said...

It should actually go on destination JSP. It should go as first statement before you do any request.getParameter(..)
I assume you have this statement in both of your JSPs:
<\%\@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" >
Make sure your browser's encoding scheme is UTF-8 before you submit. You can check this in IE, View->Encoding.
Also, check your AppServer has any default encoding schemes. If so set it to UTF-8. Hope it works for you!

Anonymous said...

Hi Venkk...
If I want doUpload and get unicode text-field in the same jsp page, how can I do right? Hope you can help me!

Disqus for techtalk