Redirect Apache Tomcat Default Page

English: Logo of Русский: Логотип Apache Tomcat

Apache Tomcat (Photo credit: Wikipedia)

Have you ever visited a web site at the root or base URL (no page or directory specified), only to see your browser automatically redirect to another directory or page? This can be done with a client-side html page that requests the browser to change location with the “meta refresh” technique. There are some other similar methods that use javascript to request the browser to load another location automatically. The technique I prefer and will show here is a server-side redirect where no page is sent back to the browser, just a simple HTTP Response header telling the browser to load a different location.

A server-side redirect can be implemented with virtually any type of web-server software. Some servers and programming languages or frameworks require a programmer to manually work with the response headers. This can be error-prone and result in poorly implemented site redirects that do not follow established standards for HTTP response headers.

Fortunately for users of the Apache Tomcat server and other standards compliant Enterprise Java web servers, a convenient redirect function is built right in. One of the easiest ways to use it is with a small JSP page. Here is an example JSP for typical server-side redirect.

<% response.sendRedirect("/some-dir/some-page.jsp"); %>

To redirect all requests from the server root (for example, http://yourserver.yourdomain.com/ ) you would put the above JSP code in a file named “index.jsp” in the Tomcat install folder for the root web-app (usually TOMCAT_INSTALL_DIR/webapps/ROOT/ ). Any user visiting your site at the root would be redirected to http://yourserver.yourdomain.com/some-dir/some-page.jsp :-).

I recommend you read the official Java documentation for the “sendRedirect” method. The documentation lists the rules you must follow when you call sendRedirect. Other tricks include automatically detecting the current application context name for redirects within a web-app, or forcing https for all redirects. Those are beyond the scope of this basic server-side redirect intro for Apache Tomcat.

Advertisements

About notesbytom

Keeping technology notes on WordPress.com to free up my mind to solve new problems rather than figuring out the same ones repeatedly :-).
This entry was posted in System Administration and tagged , , , , , . Bookmark the permalink.

One Response to Redirect Apache Tomcat Default Page

  1. Pingback: Redirect ASP.Net Default Page | Notes by Tom

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s