Joseph Victor Zammit
Joseph Victor Zammit

Reputation: 15320

Tomcat: Different welcome-file for local and live setup

I'm a beginner with Tomcat/Java; I have a simple web application for which I would like to show a different <welcome-file> in web.xml depending on whether the application is being run locally development or deployed live.

Right now I have:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

For local development I would like the <welcome-file> in web.xml to be index-dev.html for development and index-live.html for the deployed live application. Reason being I want to load different *.css and *.js depending on whether I'm local or live.

Any way in which I can achieve this setup?

Upvotes: 1

Views: 692

Answers (3)

user784540
user784540

Reputation:

As an option: make a jsp file as a welcome page.

In that jsp file analyze current client location (local computer or not), and forward to the relevant page.

ADDITIONAL EDIT:

Your jsp has a predefined request object.

Use it to get client ip, like that:

<%
    String remoteIp = request.getRemoteAddr();
%>

Compare it with localhost address, which can be obtained via:

<%
    InetAddress address = InetAddress.getLocalHost();
    String localhostIp = address.getHostAddress();
%>

And use jsp:forward to forward to a relevant page.

<%
    // getting jsp (servlet) client ip
    String remoteIp = request.getRemoteAddr();

    // getting local ip
    InetAddress address = InetAddress.getLocalHost();
    String localhostIp = address.getHostAddress();

    // checking and forwarding
    if(localhostIp.equals(remoteIp)){
%>
     <jsp:forward page="localhost.html"/>
<%}else{%>
    <jsp:forward page="remote.html"/>
<%}%>

Additional edit:

Make sure that localhostIp contains only IP address, otherwise use String methods to get substring with ip-address inside.

Upvotes: 1

Grim
Grim

Reputation: 1986

Live should always have a apache httpd who proxy-redirect to the tomcat.

This have multiple pros:

  • if the tomcat is down, you can define a "please be patient/come back later" info.html
  • using awstats you can analyze the traffic
  • You can define live JS/CSS-directorys to discharge tomcat

Regards

Upvotes: 1

Chris
Chris

Reputation: 5654

Create a header page (which is going to be included in all the pages including welcome page). Write a <c:if> to do a simple check for you. I don't think servlet spec supports different welcome pages based of requesting machine's IP address. Ideally any web-app is suppose to be serving the same pages from where ever it is accessed

Upvotes: 1

Related Questions