Discussions

Web tier: servlets, JSP, Web frameworks: Struts and UTF-8 encoded strings in fields of a form

  1. Hi
    Following codes are in one of my ActionForm classes
    -------------------------------------
    public String getFirstname() {
      return firstname;
    }
    public void setFirstname(String firstname) {
      try {
       this.firstname = new String(firstname.getBytes("UTF-8"), "UTF-8");
        }
        catch (UnsupportedEncodingException ex) {
        }
      }
    -----------------------------------------
  2. ***** The first message was incomplete ******
      ***** Here is the corrected one ******
    Hi
    Following codes are in one of my ActionForm classes
    -------------------------------------
    public String getFirstname() {
      return firstname;
    }
    public void setFirstname(String firstname) {
      try {
       this.firstname = new String(firstname.getBytes("UTF-8"), "UTF-8");
      }
      catch (UnsupportedEncodingException ex) {
      }
    }
    -----------------------------------------
    After using input field (named firstname) of the relevant <html:form> fields in order to add a row to DB, content of this field are not inserted to the DB correctly. This content is in Persian(Farsi). I mean instead of a persian word, there are some unknown symbols in the database.
    I want to know if there is a sure way to make the encoding of the content UTF-8 for all phases of processing.

    Aside: I'm sure there's something done undesireably about the encoding of the content before ActionClass.execute(...) is called.

    Thanks
  3. Just to make sure, check your database character set encoding also. If you use oracle this might help you: http://dnaugler.cs.semo.edu/oracledocs/a90842/ch12.htm

    Senthil.
  4. I'm sure it's not caused by anything but the ActionForm.
    When user types persian words in the field, if the validate method returns non-empty ActionErrors, some unknown symbols will be shown instead of the persian words.

    Thanks again
    Majid Makki
  5. It's solved[ Go to top ]

    It's solved by adding the following code fragment to the begining of reset(,) and validate(,) methods in ActionForm:
    _______________________________________
        try {
          request.setCharacterEncoding("UTF-8");
        }
        catch (UnsupportedEncodingException ex) {
        }
    ________________________________________

    Thanks for your care