简体   繁体   English

Tomcat:本地和实时设置的不同欢迎文件

[英]Tomcat: Different welcome-file for local and live setup

I'm a beginner with Tomcat/Java; 我是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. 我有一个简单的Web应用程序,我想在web.xml显示不同的<welcome-file> ,具体取决于应用程序是在本地开发还是实时部署。

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. 对于地方发展我想<welcome-file>web.xmlindex-dev.html发展和index-live.html的部署实时应用。 Reason being I want to load different *.css and *.js depending on whether I'm local or live. 原因是我想加载不同的*.css*.js取决于我是本地的还是现场的。

Any way in which I can achieve this setup? 我可以通过哪种方式实现此设置?

As an option: make a jsp file as a welcome page. 作为选项:将jsp文件作为欢迎页面。

In that jsp file analyze current client location (local computer or not), and forward to the relevant page. 在该jsp文件中分析当前客户端位置(本地计算机与否),并转发到相关页面。

ADDITIONAL EDIT: 其他编辑:

Your jsp has a predefined request object. 您的jsp有一个预定义的request对象。

Use it to get client ip, like that: 用它来获取客户端ip,就像这样:

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

Compare it with localhost address, which can be obtained via: 将它与localhost地址进行比较,可以通过以下方式获得:

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

And use jsp:forward to forward to a relevant page. 并使用jsp:forward转发到相关页面。

<%
    // 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. 确保localhostIp仅包含IP地址,否则使用String方法获取带有ip-address的子字符串。

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. 写一个<c:if>来做一个简单的检查。 I don't think servlet spec supports different welcome pages based of requesting machine's IP address. 我不认为servlet规范支持基于请求机器的IP地址的不同欢迎页面。 Ideally any web-app is suppose to be serving the same pages from where ever it is accessed 理想情况下,任何web-app都应该从访问它的位置提供相同的页面

Live should always have a apache httpd who proxy-redirect to the tomcat. Live应该总是有一个apache httpd who proxy-redirect to tomcat。

This have multiple pros: 这有多个优点:

  • if the tomcat is down, you can define a "please be patient/come back later" info.html 如果tomcat关闭,你可以定义一个“请耐心等待/稍后回来”info.html
  • using awstats you can analyze the traffic 使用awstats你可以分析流量
  • You can define live JS/CSS-directorys to discharge tomcat 您可以定义实时JS / CSS-directorys来释放tomcat

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM