You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by wessam <we...@asset.com.eg> on 2008/01/23 13:52:43 UTC

struts request encoding to utf-8 problem

i'm using struts 1.1, oracle 10g environment
i'm facing a problem that i can't save special characters as "ẻ, €" from the
page to database correctly though i used all the possible ways to set the
encoding to utf-8
her's the code i used
1- in the.jsp page ..
  <%@ page language="java" pageEncoding="UTF-8"
contentType="text/html;charset=UTF-8" %>

 and in the header ..
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

and declare the form ..
  <form method="post" accept-charset="UTF-8" action="<%=actionName%>">

2- i used an encoding filter ...

package controller;

import 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;


public class EncodingFilter implements Filter {

  private String encoding = "UTF-8";

  public void doFilter(ServletRequest request,
      ServletResponse response, FilterChain filterChain)
      throws IOException, ServletException {

    request.setCharacterEncoding(encoding);
    filterChain.doFilter(request, response);
  }

  public void init(FilterConfig filterConfig)
	           throws ServletException {
    String encodingParam = filterConfig
              .getInitParameter("encoding");
    if (encodingParam != null) {
      encoding = encodingParam;
    }
  }

  public void destroy() {
    // nothing todo
  }
}

and declared it as following in web.xml ...
  <?xml version = '1.0' encoding = 'UTF-8'?>

and ..

  <filter>
      <filter-name>EncodingFilter</filter-name> 
      <filter-class>controller.EncodingFilter</filter-class> 
     <init-param>
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>EncodingFilter</filter-name> 
      <url-pattern>/*</url-pattern> 
    </filter-mapping>

3- in struts-config ...
  <?xml version="1.0" encoding= "UTF-8" ?>

and ..
  <controller  contentType="text/html; charset=UTF-8" 
               
multipartClass="org.apache.struts.upload.CommonsMultipartRequestHandler"
                nocache="true" />

the point is the the characters reaches the action class corectly but when i
save to datbase an retrieve again it's viewed corrupted in the page 

any help please??
  
-- 
View this message in context: http://www.nabble.com/struts-request-encoding-to-utf-8-problem-tp15041079p15041079.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts request encoding to utf-8 problem

Posted by wessam <we...@asset.com.eg>.
i did add   response.setCharacterEncoding(encoding), in EncodingFilter class
but still output corrupted characters in the jsp
:(
i really need to solve this problem as soon as possible .. any help please ?

-- 
View this message in context: http://www.nabble.com/struts-request-encoding-to-utf-8-problem-tp15041079p15041950.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts request encoding to utf-8 problem

Posted by Laurie Harper <la...@holoweb.net>.
wessam wrote:
> i'm using struts 1.1, oracle 10g environment
> i'm facing a problem that i can't save special characters as "ẻ, €" from the
> page to database correctly though i used all the possible ways to set the
> encoding to utf-8
>
 > [...]
> 
> the point is the the characters reaches the action class corectly but when i
> save to datbase an retrieve again it's viewed corrupted in the page 


You've partially narrowed down the problem, which is good; now you need 
to narrow it down the rest of the way:

- is the data written to the database correctly? (try verifying outside 
or Struts)

- is the data retrieved from the database correctly in your action?

In other words, you need to determine if the problem is with the save to 
  or restore from the database, or whether the problem is with 
displaying the data from Struts.

My guess would be that it's the save/retrieve, since you didn't mention 
anything you'd done to ensure communication with the database was 
preserving correct character encoding, in which case it would be outside 
the scope of Struts.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts request encoding to utf-8 problem

Posted by Robert Slama <rs...@spiritline.org>.
try :

        request.setCharacterEncoding(encoding);
        response.setCharacterEncoding(encoding);
        filterChain.doFilter(request, response);

r^


S pozdravom Robert Slama

SpiritLine s.r.o.
Bernolakova ul. 1A
901 01 Malacky

rslama@spiritline.org
gsm: +421 905 122 841
tel: +421 34 778 20 88
     +421 34 778 20 89
fax: +421 34 778 20 90

http://www.spiritline.org



wessam  wrote / napísal(a):
> i'm using struts 1.1, oracle 10g environment
> i'm facing a problem that i can't save special characters as "ẻ, €" from the
> page to database correctly though i used all the possible ways to set the
> encoding to utf-8
> her's the code i used
> 1- in the.jsp page ..
>   <%@ page language="java" pageEncoding="UTF-8"
> contentType="text/html;charset=UTF-8" %>
>
>  and in the header ..
> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
>
> and declare the form ..
>   <form method="post" accept-charset="UTF-8" action="<%=actionName%>">
>
> 2- i used an encoding filter ...
>
> package controller;
>
> import 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;
>
>
> public class EncodingFilter implements Filter {
>
>   private String encoding = "UTF-8";
>
>   public void doFilter(ServletRequest request,
>       ServletResponse response, FilterChain filterChain)
>       throws IOException, ServletException {
>
>     request.setCharacterEncoding(encoding);
>     filterChain.doFilter(request, response);
>   }
>
>   public void init(FilterConfig filterConfig)
> 	           throws ServletException {
>     String encodingParam = filterConfig
>               .getInitParameter("encoding");
>     if (encodingParam != null) {
>       encoding = encodingParam;
>     }
>   }
>
>   public void destroy() {
>     // nothing todo
>   }
> }
>
> and declared it as following in web.xml ...
>   <?xml version = '1.0' encoding = 'UTF-8'?>
>
> and ..
>
>   <filter>
>       <filter-name>EncodingFilter</filter-name> 
>       <filter-class>controller.EncodingFilter</filter-class> 
>      <init-param>
>       <param-name>encoding</param-name> 
>       <param-value>UTF-8</param-value> 
>       </init-param>
>     </filter>
>     <filter-mapping>
>       <filter-name>EncodingFilter</filter-name> 
>       <url-pattern>/*</url-pattern> 
>     </filter-mapping>
>
> 3- in struts-config ...
>   <?xml version="1.0" encoding= "UTF-8" ?>
>
> and ..
>   <controller  contentType="text/html; charset=UTF-8" 
>                
> multipartClass="org.apache.struts.upload.CommonsMultipartRequestHandler"
>                 nocache="true" />
>
> the point is the the characters reaches the action class corectly but when i
> save to datbase an retrieve again it's viewed corrupted in the page 
>
> any help please??
>   
>   

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts request encoding to utf-8 problem

Posted by wessam <we...@asset.com.eg>.



In this case, you've isolated your problem to the database read/write  
phase, which means it's not a Struts issue. You'll need to look again  
at your database configuration and whatever middleware you're using  
to communicate with it (e.g. JDBC, Hibernate). The specifics will  
depend on your database backend, the middleware you're using and your  
own persistence code and configuration.

L.


well i don't think that's the problem in database configuration as there're
other projects works with it fine
concerning middleware, i'm using JDBC
but i donno what should i do to make JDBC middleware compatible with utf-8
encoding

here's a snapet of JDBC connection i used ..

 public Connection dbConnect(String db_connect_string,String db_userid,
String db_password)
 {
     try
     {
         Connection conn = DriverManager.getConnection( db_connect_string,
db_userid, db_password);
         System.out.println("connected");
         return conn;
     }
     catch (Exception e)
     {
         e.printStackTrace();
         return null;
     }
 }
 
 public String getText()
 {
     String text = "";
     Statement statement = null;
  try
  {
     Connection conn = dbConnect("jdbc:oracle:thin:@localhost:1522:fat10g",
"test", "t");
     statement = conn.createStatement();
     String query = "select * from test";
     
     ResultSet resultSet = statement.executeQuery ( query ) ;
     // get the last entered row in test table
     while ( resultSet.next () )
     {
      text = resultSet.getString("test");
     }
    // text = new String(text.getBytes(), "UTF-8");
     
  }
     catch(Exception e)
     {
         e.printStackTrace();
     }
     finally //ensure statement is closed properly
          {
               try
                    {
                         statement.close () ;
                    }
               catch ( SQLException e )
                    {
                         
                    }
          }
     return text;
 }
 
 public void saveText(String text)
 {
     Statement statement = null;
     try
     {
        Connection conn =
dbConnect("jdbc:oracle:thin:@localhost:1522:fat10g", "test", "t");
         statement = conn.createStatement();
        String query = "insert into TEST (test) values ('" + text + "')";
        boolean result = statement.execute( query ) ;
     }
     catch(SQLException  e)
     {
        e.printStackTrace();
     }
     finally //ensure statement is closed properly
      {
       try
        {
          statement.close () ;
        }
       catch ( SQLException e )
        {}
      }
 }

what should i do to middleware ?
-- 
View this message in context: http://www.nabble.com/struts-request-encoding-to-utf-8-problem-tp15041079p15061172.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org