Getting Wordpress to Work in Tomcat (PHP in a Java Environment)

Getting Wordpress to work in Tomcat (or another java servlet container) is easier than you think! For a proof of concept look no further. The original Siphs site was designed in JSP and Java, while the current page is rendered by Wordpress. Here’s how we did it.
Setting Up Wordpress in Tomcat
Download Required Packages
- Get the latest version of wordpress from wordpress.org
- Get the latest version of the Quercus, an open source Java implementation of PHP 5 from http://quercus.caucho.com/. You are going to download the .war file found at the bottom of the page.
- Get the latest mysql java connector
Modify Tomcat
- Extract quercus.jar, resin-util.jar, and script10.jar from the quercus.war file and upload them to your Tomact common/lib folder.
- Add the latest mysql-connector-java.jar file to your common/lib folder.
- Next modify your webapp’s web.xml file. Add the following:
<servlet>
<servlet-name>com.caucho.quercus.servlet.QuercusServlet</servlet-name> <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.caucho.quercus.servlet.QuercusServlet</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>
- If you are not running Apache and mod_rewrite, the Wordpress permalinks structure simply won’t work and without the following modification to Tomcat neither will the parameterized urls. To get around this, we modified the tomcat/conf/web.xml file to the following.
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.php</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Modify Wordpress
- Open wordpress/wp-includes/formatting.php
- Change the js_escape() function on line 1171 to:
function js_escape($text) {
$safe_text = wp_specialchars($text, 'double');
//$safe_text = preg_replace('/(x)?0*(?(1)27|39);?/i', "'", stripslashes($safe_text));
//$safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
//stripslashes($safe_text));
$safe_text = preg_replace("/\r/", "\n", addslashes($safe_text));
$safe_text = preg_replace("/\n/", "\n", addslashes($safe_text));
return apply_filters('js_escape', $safe_text, $text);
- Change the wordpress/wp-config.php file to reflect your database settings.
And that’s it!
Bounce (shutdown/start) your Tomact Instance. Then, navigate to <your URL>/wp-admin/config.php and install wordpress. That should do the trick. Feel free to leave some comments below if you need any help!

