-2

I have a Java Servlet which accepts data from HTML through ajax javascript. Html receives hindi text from user and sends it through ajax into a servlet. I have specified UTF-8 format everywhere but it is not working.

I have set request and response encoding as utf-8 , still not working. The server is Tomcat.

If anyone can help.

Yasha Jadwani
  • 45
  • 1
  • 2
  • 8
  • 1
    Share your code and request response.FYI, tomcat has no such limitation. – Juned Ahsan Dec 17 '15 at 05:08
  • I have a simple java servlet, with request and response set to UTF-8 format. – Yasha Jadwani Dec 17 '15 at 05:20
  • 1
    And how do you test it? Why do you think the response is not in UTF-8 format? – Juned Ahsan Dec 17 '15 at 05:21
  • i am getting text in servlet from html and i fire a sql query to fetch data from data base. The data from dtabase comes null because the text whhich goes is not in hindi hence the sql "like" query does not work. – Yasha Jadwani Dec 17 '15 at 05:23
  • 1
    It looks you need to specify UTF-8 in connection string too , see my answer , [Answer](http://stackoverflow.com/questions/34113606/how-can-i-show-arabic-query-search-from-mysql-by-javafx/34131549#34131549) This is just a guess since you have not shared your code. – Sabir Khan Dec 17 '15 at 06:35
  • please tell that connection string for Hindi in databse – Yasha Jadwani Dec 17 '15 at 08:52
  • Thanks that worked for me.. – Yasha Jadwani Dec 17 '15 at 09:03
  • Since a Java+MySQL answer solved your question, this is a duplicate of: http://stackoverflow.com/questions/8433293/how-can-i-insert-arabic-word-to-mysql-database-using-java – Alastair McCormack Dec 17 '15 at 18:06

1 Answers1

2

Follow the following steps to configure your Tomcat to serve your UTF-8 pages correctly either this is Persian, Arabic or any language that is written from right to left:

(1) Edit your server.xml as following

2) CharsetFilter force the java webapp to handle all requests and responses as UTF-8 encoded. This requires that we define a character set filter like the following:

package charsetFilter.classes;

 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 CharsetFilter implements Filter{
    private String encoding;

    public void init(FilterConfig config) throws ServletException{
            encoding = config.getInitParameter("requestEncoding");
            if( encoding==null ) encoding="UTF-8";
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain       next)
    throws IOException, ServletException{           
            if(null == request.getCharacterEncoding())
            request.setCharacterEncoding(encoding);             
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
            next.doFilter(request, response);
    }

        public void destroy(){}
}   

This filter makes sure that if the browser hasn't set the encoding used in the request, that it's set to UTF-8. The other thing done by this filter is to set the default response encoding ie. the encoding in which the returned html/whatever is. The alternative is to set the response encoding etc. in each controller of the application.

3) Then add this filter to the web.xml like:

CharsetFilter charsetFilter.classes.CharsetFilter requestEncoding UTF-8

<filter-mapping>
        <filter-name>CharsetFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

Then write your servlet as:

request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");

and use whatever language you want to use in your servlet.

You can use this for getting parameters:

String input = new String(request.getParameter("foo").getBytes("iso-8859-1"), "utf-8");

Cheers!!

Ghayel
  • 1,113
  • 2
  • 10
  • 19