Yan Sheng:

You are dealing two issues here. 

The first one is to ensure proper encoding for input String data. So that you store 
and retrieve the string data in backend DB with the right encoding. The encoding 
filter is for this purpose. The filter declaration in your web.xml shall be something 
like

  <!-- Encoding Filter Declaration Start -->
  <filter>
    <filter-name>EncodingFilter</filter-name>
    <display-name>EncodingFilter</display-name>
    <description>no description</description>
    <filter-class>....EncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>

The second issue you are dealing with is the locale setting in your application so 
that non-European languages can be shown properly. From your code segment, you use 
Struts if my guess is right. Please consult the framework document on the topic, that 
is how to set locale.

Hope this helps.


Vernon
--

--------- Original Message ---------

DATE: Thu, 4 Dec 2003 15:23:52 
From: "Yansheng Lin" <[EMAIL PROTECTED]>
To: "'Tag Libraries Users List'" <[EMAIL PROTECTED]>
Cc: 

>Um, I agree. But...
>
>Ok, I just quickly restarted Tomcat with a breakpoint in init(), and I realized
>I made a mistake by saying ApplicationFilterConfig would be null when deploying.
>It's the initial encoding that is null...
>       ApplicationFilterConfig.getInitParameter("encoding") == null
>So I have to put an if-else in the init to default the encoding to 'UTF-8;
>
>But in the beginning of my dispatchMethod, the debug output still doesn't make
>sense...
>
>        System.out.println(
>            "ManageTranslateAction: dispatchMethod --> action=" + action);
>        System.out.println(request.getCharacterEncoding());
>        System.out.println(request.getContentType());
>        System.out.println(request.getLocale());    
>
>------
>ManageTranslateAction: dispatchMethod --> action=default
>null
>null
>ja
>------
>
>Doh!
>
>-----Original Message-----
>From: Adam Hardy [mailto:[EMAIL PROTECTED] 
>Sent: Thursday, December 04, 2003 3:04 PM
>To: Tag Libraries Users List
>Subject: Re: Request encoding problem
>
>
>As long as you set the filter parameter in the web.xml, then the filter 
>config will be valid, not null.
>
>Adam
>
>On 12/04/2003 10:51 PM Yansheng Lin wrote:
>> Please bear with me for a moment here, I am a bit confused at how the
>doFilter()
>> works.  In servlet javadoc:
>> 
>>      The doFilter method of the Filter is called by the container each time a
>> request/response pair is passed through the chain due to a client request for
>a
>> resource at the end of the chain. The FilterChain passed in to this method
>> allows the Filter to pass on the request and response to the next entity in
>the
>> chain.
>> 
>> Let's see whether I understand correctly. So the init() method is called when
>> the application deploys.  But since congif is null at that time, the
>> targetEncoding is set to null in the init().
>> As is reflected in 
>>      this.targetEncoding = config.getInitParameter("encoding");
>> 
>> 
>> So when I try to do the Filter, since the request doesn't have any encoding
>> information, the
>>       request.setCharacterEncoding(targetEncoding); 
>> is going to set the charater encoding to null.
>> 
>> And that's exactly what I got:
>> java.lang.NullPointerException: charsetName
>>      at java.lang.String.(String.java:329)
>>      at java.lang.String.(String.java:359)
>>      at
>>
>org.apache.coyote.tomcat4.CoyoteRequest.setCharacterEncoding(CoyoteRequest.java:
>> 1198)
>>      at
>>
>org.apache.coyote.tomcat4.CoyoteRequestFacade.setCharacterEncoding(CoyoteRequest
>> Facade.java:157)
>>      at org.j2ee_test.util.EncodingFilter.doFilter(EncodingFilter.java:52)
>>      at
>>
>org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt
>> erChain.java:213)
>>      at
>>
>org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.
>> java:193)
>>      ...
>> 
>> So if I default 
>>      request.setCharacterEncoding("UTF-8"); 
>> Everything works fine.
>> 
>> So I guess what I don't understand is when you have the character encoding
>> attribute for a page.
>> It makes sense to me that when processing the request, Filter should already
>> know what kind of targetEncoding it is.  But the example code below obviously
>> does not provide that mechanism.  
>> 
>> Here is what I have in my web.xml, no sure if it helps:
>> 
>>   <filter>
>>     <filter-name>EncodingFilter</filter-name>
>>     <display-name>EncodingFilter</display-name>
>>     <description></description>
>>     <filter-class>org.j2ee_test.util.EncodingFilter</filter-class>
>>   </filter>
>>   
>>   <filter-mapping>
>>     <filter-name>EncodingFilter</filter-name>
>>     <url-pattern>/</url-pattern>
>>   </filter-mapping>
>> 
>> Again thanks for the help.
>> 
>> 
>> -----Original Message-----
>> From: Vernon Smith [mailto:[EMAIL PROTECTED] 
>> Sent: Thursday, December 04, 2003 1:35 PM
>> To: Tag Libraries Users List
>> Subject: Re: Request encoding problem
>> 
>> 
>> You also need to set up the input field encoding in addition of page character
>> set. You can do so field by field to convert the input string encoding or
>using
>> the following filter from the PetStore.
>> 
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> mport java.io.IOException;
>> 
>> import javax.servlet.Filter;
>> import javax.servlet.FilterChain;
>> import javax.servlet.FilterConfig;
>> import javax.servlet.ServletException;
>> import javax.servlet.ServletRequest;
>> import javax.servlet.ServletResponse;
>> import javax.servlet.http.HttpServletRequest;
>> 
>> 
>> public class EncodingFilter implements Filter {
>> 
>>     // default to ASCII
>>     private String targetEncoding = "ASCII";
>> 
>>     public void init(FilterConfig config) throws ServletException {
>>         this.targetEncoding = config.getInitParameter("encoding");
>>     }
>> 
>>     public void destroy() {
>>         targetEncoding = null;
>>     }
>> 
>>      public  void doFilter(ServletRequest srequest, ServletResponse
>sresponse,
>> FilterChain chain)
>>         throws IOException, ServletException {
>> 
>>         HttpServletRequest request = (HttpServletRequest)srequest;
>>         request.setCharacterEncoding(targetEncoding);
>>         // move on to the next
>>        chain.doFilter(srequest,sresponse);
>>     }
>> }
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> And setup this filter for all JSP files in your web.xml.
>> 
>> Hope this helps.
>> 
>> 
>> --
>> 
>> --------- Original Message ---------
>> 
>> DATE: Thu, 4 Dec 2003 12:58:54 
>> From: "Yansheng Lin" <[EMAIL PROTECTED]>
>> To: "'Tag Libraries Users List'" <[EMAIL PROTECTED]>
>> Cc: 
>> 
>> 
>>>Hi,
>>>
>>>I have trouble processing user's input using UTF-8 encoding.  I can change to
>>>different locales with 
>>> <fmt:setLocale value='${sessionScope["org.apache.struts.action.LOCALE"]}'/>, 
>>>but I don't think 
>>> <fmt:requestEncoding value='UTF-8'/> 
>>>is working for me right now. I already set the page encoding in my hearder
>> 
>> with:
>> 
>>> <meta http-equiv="content-type" content="text/html; charset=UTF-8">
>>>
>>>Here is my Systen.out in the beginning of dispatchAction():
>>>
>>>       System.out.println(request.getCharacterEncoding());
>>>       System.out.println(request.getContentType());
>>>       System.out.println(request.getLocale());        
>>>
>>>And this is what I get:
>>>       null
>>>       application/x-www-form-urlencoded
>>>       en_US
>>>
>>>I am not sure what's missing right now.  Do I have to set anything in the
>>>web.xml?
>>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
>



____________________________________________________________
Get advanced SPAM filtering on Webmail or POP Mail ... Get Lycos Mail!
http://login.mail.lycos.com/r/referral?aid=27005

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to