How To: Tomcat Applications With Multiple Folders

In a previous post, I asked about setting up Tomcat with multiple root folders and a single application. Use this recipe if
  • you are deploying a single application as a .war file
  • you need multiple root folders for your application
  • you store data in HttpSession
  • you have the Apache WS with mod_rewrite enabled
Recipe

  1. Open your server.conf file in CATALINA_HOME/conf and add the following parameter to your Connector setting emptySessionPath="true". This setting will force Tomcat to use '/' as the Cookie Path for JSESSIONID, the cookie used for sessions.
  2. Open your httpd.conf file. First, you'll need to add additional proxy statements:
ProxyPass /newpath http://localhost:8080/applicationpath ProxyPassReverse /newpath http://localhost:8080/applicationpath

newpath
is the new folder you want to setup.
applicationpath is the name of your war file

Note: I am assuming you're running apache and Tomcat on the same server. If not, replace localhost with your Tomcat host.

3. Add the following rewrite rule to your httpd.conf file

RewriteRule ^/newpath/(.*) /applicationpath/$1

4. Write your URLs correctly using the /newpath


Why this works?


You've forced tomcat to set the Session's cookie path to '/', so no matter what directories you create, your application will still receive the session cookie. The proxy statement tells Apache about the new folder to map to your application. The rewrite rule makes tomcat think that the URI is for your application.

What else?

In my next report, I'll tell you how to make sure your URLs are unique, something that is important for search engine optimization (SEO).

1 comment:

  1. Thanks for the emptySessionPath tip, exactly what I needed.

    For directory rewrites I always like to leave the trailing slash as part of the backreference, for example

    RewriteRule ^/newpath(/.*) /applicationpath$1

    That way both 'newpath' and 'newpath/' work.

    ReplyDelete

Comments on this blog are moderated and we do not accept comments that have links to other websites.

Share

About Isaac Sacolick

Isaac Sacolick is President of StarCIO, a technology leadership company that guides organizations on building digital transformation core competencies. He is the author of Digital Trailblazer and the Amazon bestseller Driving Digital and speaks about agile planning, devops, data science, product management, and other digital transformation best practices. Sacolick is a recognized top social CIO, a digital transformation influencer, and has over 900 articles published at InfoWorld, CIO.com, his blog Social, Agile, and Transformation, and other sites. You can find him sharing new insights @NYIke on Twitter, his Driving Digital Standup YouTube channel, or during the Coffee with Digital Trailblazers.