UTF8 JDBC on Tomcat

I’ve had opportunity to once again visit the UTF8 chain of failure and thought I’d write about it. If for no other reason, it’s easier for me to find my notes when I shove them into a blog entry.

I previously wrote about UTF8 on Tomcat. I pointed out that I needed to add an attribute to the connector element so that the mod_jk connection would be UTF8-ified. I neglected to also point out that I needed to UTF8-ified the database connection.

jdbc:mysql://localhost/mywebapp?useUnicode=true&characterEncoding=utf8

I’ve included my entire Tomcat server.xml file to illustrate just where this stuff goes. I don’t use port 8080; all my traffic comes through the mod_jk connector. Also, I don’t claim that I know what everything in my config file does. Most of everything: yes. All of everything: no.

I do claim that this server.xml has given me trouble free service for more than two years.

<Server port="8005" shutdown="SHUTDOWN">

<Listener
  className="org.apache.catalina.core.AprLifecycleListener" />

<Listener
  className="org.apache.catalina.mbeans.ServerLifecycleListener" />

<Listener
  className="org.apache.catalina.mbeans↩
.GlobalResourcesLifecycleListener" />

<Listener
  className="org.apache.catalina.storeconfig↩
.StoreConfigLifecycleListener"/>


<GlobalNamingResources>

  <Environment
    name="simpleValue"
    type="java.lang.Integer"
    value="30"/>

  <Resource
    name="UserDatabase"
    auth="Container"
    type="org.apache.catalina.UserDatabase"
    description="User database that can be updated and saved"
    factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname="conf/tomcat-users.xml" />

</GlobalNamingResources>

<Service name="Catalina">

  <Connector
    pollerSize="1024"
    port="8009" 
    URIEncoding="UTF-8"
    enableLookups="false"
    redirectPort="8443"
    maxPostSize="104857600"
    protocol="AJP/1.3" />

  <Engine name="Catalina" defaultHost="www.mywebapp.com">

    <Host
      name="www.mywebapp.com"
      appBase="webapps"
      debug="4"
      unpackWARs="true">

      <Alias>www.mywebapp.com</Alias>

      <Valve
        className="org.apache.catalina.valves.AccessLogValve"
        directory="logs/mywebapp"
        prefix="access."
        suffix=".log"
        pattern="common"/>

      <Logger
        className="org.apache.catalina.logger.FileLogger"
        directory="logs/mywebapp"
        prefix="host."
        suffix=".log"
        verbosity="debug"
        timestamp="true"/>

      <Context
        path=""
        docBase="mywebapp"
        debug="4"
        reloadable="true">

        <Logger
          className="org.apache.catalina.logger.FileLogger"
          directory="logs/mywebapp"
          prefix="context."
          suffix=".log"
          timestamp="true"/>

        <Resource
          name="jdbc/mywebapp"
          auth="Container"
          type="javax.sql.DataSource"
          driverClassName="com.mysql.jdbc.Driver"
          username="somename"
          password="somepass"
          url="jdbc:mysql://localhost/mywebapp?useUnicode=true↩
&amp;characterEncoding=utf8" />

        <Resource
          name="mail/Session"
          auth="Container"
          type="javax.mail.Session"
          mail.smtp.host="localhost" />

      </Context>

    </Host>

  </Engine>

</Service>

</Server>

Your email will never published nor shared. Required fields are marked *...

*

*

Type your comment out: