0

I am trying to write russian characters to MySQL 5.5 using JSP (JSTL). While it works on my own localhost, I can't get it to work on the server I rented.

It's sending the right params from a form to another page, which then shows the output correctly. This also works on the server. Writing/Reading to and from DB only works on my own localhost.

If I try to write to DB on the server, russian characters turn into question marks. However, I can store russian characters using phpMyAdmin, which then display correctly on my web page.

Here is my query and what I've tried so far: Query:

    <sql:update dataSource="${ds}" var="updatedTable">
        INSERT INTO Question (question) VALUES (?) 
        <sql:param value="${param.questionAsked}" />
    </sql:update>

DBconn:

    <sql:setDataSource var="ds" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/myDBname?useUnicode=true&characterEncoding=utf-8" user="secret" password="secret" />

Page Encoding in every JSP file:

    <%@page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Add this to tomcat server.xml:

    <Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="UTF-8"
/>

Uncommented the CharacterEncodingFilter in web.xml:

    <filter>
    <filter-name>setCharacterEncodingFilter</filter-name>
    <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <async-supported>true</async-supported>
</filter>
    <filter-mapping>
    <filter-name>setCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And altering/creating new DB's and DB tables for test with utf8:

CREATE TABLE `Question` (
  `questionID` int(6) unsigned NOT NULL AUTO_INCREMENT,
  `question` varchar(255) DEFAULT NULL,
  `categoryID` int(6) unsigned DEFAULT NULL,
  PRIMARY KEY (`questionID`),
  KEY `categoryID` (`categoryID`),
  CONSTRAINT `question_ibfk_1` FOREIGN KEY (`categoryID`) REFERENCES `Category` (`categoryID`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;

I guess my problem is somewhere in the MySQL connector. I could not quite figure out how to change it since I don't have access to the config files of mysql. I requested SSH access if that helps me. I'm fairly new to such issues since I haven't been dealing with databases and especially servers for too long.

phpMyAdmin shows:

character set client - latin1, (session value - utf8)
character set connection - latin1, (session value - utf8)
character set database - latin1
character set results - latin1, (session value - utf8)
character set server - latin1
character set system - utf8
collation connection - latin1_swedish_ci (session value - utf8_unicode_ci)
collation database - latin1_swedish_ci
collation server - latin1_swedish_ci

I hope you can suggest what to do...

1
  • Thanks for your help Balus, I could figure it out by using a servlet to make sure utf8 goes in and out.
    – Julian Ka
    Commented Jun 28, 2015 at 14:06

1 Answer 1

0

I figured it out myself. Just uncommenting the CharacterEncoding filter wasn't enough. I had to implement a filter into my webapp as well. Heres the code:

package filters;

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 {
    // Respect the client-specified character encoding
    // (see HTTP specification section 3.4.1)
    if (null == request.getCharacterEncoding())
        request.setCharacterEncoding(encoding);

    /**
     * Set the default response content type and encoding
     */
    response.setContentType("text/html; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");

    next.doFilter(request, response);
}

public void destroy() {
}

}

And don't forget it in the web.xml:

   <filter>
   <filter-name>CharsetFilter</filter-name>
   <filter-class>filters.CharsetFilter</filter-class>
   <init-param>
    <param-name>requestEncoding</param-name>
    <param-value>UTF-8</param-value>
   </init-param>
   </filter>
   <filter-mapping>
   <filter-name>CharsetFilter</filter-name>
   <url-pattern>/*</url-pattern>
   </filter-mapping>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.