samedi 27 juin 2015

(JSP) Writing UTF8 to MySQL won't work

I am stuck for days and I can't find a suitable post here.

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 guys know what to do...

Aucun commentaire:

Enregistrer un commentaire