This is not a bug in JBoss.  This is related to the way of handling charcters 
encoding in Servlet and JSP.  Your application works in WAS, because WAS has 
implemented its own way of handling characters in HTML request.

Problem 1: When a form is submitted, the browser will encode the characters in 
the character encoding set in the browser at that moment.  The default encoding 
is the encoding at which the page is displayed but this can be changed by the 
user easily.

Problem 2: When the Servlet container receives the request, it always pass the 
request parameters to your program decoded in ISO-8859-1 encoding. (e.g. 
Browser encoded in UTF-8 but container decoded in ISO-8859-1.)  Thus your 
servlet or JSP will always receive garbage for characters other than ISO-8859-1 
encoding.

To solve problem 1, you have to add the accept-encoding option in the html form 
tag

<form action="abc.do" method="post" accept-encoding="UTF-8">

or if you are using Struts, use the accpet-charset

<html:form action="abc" accept-charset="UTF-8">

The accept-encoding option will tell the browser always encode the character in 
the specified encoding and ignore what the user has set.

For problem 2, even when the form has accept-encoding, when you issue 
request.getParameter(), you still get the character decoded in ISO-8859-1.  To 
get the correct encoding, you should do as follows when you issue the 
request.getParameter()

String value = request.getParameter("mytext");
  | try{
  |     value = new String(value.getBytes("8859_1"), "UTF-8");
  | }catch(java.io.UnsupportedEncodingException e){
  |     System.err.println(e);
  | }

Although this decode the character using the correct encoding, the problem 
arises when you upload a file with filename not in ISO-8859-1 encoding.  
Because you cannot decode the filename correctly using the above method.

So the best method at this moment is to use a filter to do the decoding.  But 
filter is not available before Servlet 2.3.

Add the following in web.xml

<!-- This filter should be placed at beginning of web.xml so that the request 
is not accessed yet -->
  | <filter>
  |   <display-name>set character encoding</display-name>
  |   <filter-name>setCharacterEncodingFilter</filter-name>
  |   
<filter-class>your.package.web.filter.SetCharacterEncodingFilter</filter-class>
  |   <init-param>
  |     <param-name>encoding</param-name>
  |     <param-value>UTF-8</param-value>
  |   </init-param>
  | </filter>
  | 
  | <filter-mapping>
  |   <filter-name>setCharacterEncodingFilter</filter-name>
  |   <url-pattern>/*</url-pattern>
  | </filter-mapping>

And create the filter as follows

package your.package.web.filter;
  | 
  | import java.io.IOException;
  | import javax.servlet.*;
  | import javax.servlet.http.*;
  | 
  | /**
  |  * Set the character encoding of the request to as set in "encoding"
  |  * this should be done before the request is accessed.
  |  */
  | public class SetCharacterEncodingFilter implements Filter {
  | 
  |    private FilterConfig filterConfig;
  |    private String encoding;
  | 
  |    public void init(FilterConfig filterConfig) {
  |       this.filterConfig = filterConfig;
  |       this.encoding = filterConfig.getInitParameter("encoding");
  |    }
  |    public void doFilter(ServletRequest  request,
  |                         ServletResponse response,
  |                         FilterChain     chain) throws IOException, 
ServletException {
  |       request.setCharacterEncoding(this.encoding);
  |       chain.doFilter(request, response);
  |    }
  |    public void destroy() {
  |       this.filterConfig = null;
  |    }
  | }

References:
http://java.sun.com/products/servlet/Filters.html
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Servlets8.html#wp64572

Thanks
C. N.

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3952987#3952987

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3952987

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
JBoss-user mailing list
JBoss-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to