修改嵌入式tomcat webapp的配置

发布于 2024-12-21 22:12:35 字数 1364 浏览 0 评论 0原文

我一直在尝试修改我的heroku 应用程序的嵌入式tomcat 配置。我已经使用下面的 wiki 链接安装了 heroku 应用程序,该链接配置了一个简单的嵌入式 tomcat。

http://devcenter.heroku.com/articles /create-a-java-web-application-using-embedded-tomcat

源代码在这里:

public static void main(String[] args) throws Exception {

    String webappDirLocation = "src/main/webapp/";
    Tomcat tomcat = new Tomcat();

    //The port that we should run on can be set into an environment variable
    //Look for that variable and default to 8080 if it isn't there.
    String webPort = System.getenv("PORT");
    if(webPort == null || webPort.isEmpty()) {
        webPort = "8080";
    }

    tomcat.setPort(Integer.valueOf(webPort));

    tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
    System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

    tomcat.start();
    tomcat.getServer().await();  

}

问题:

  1. 由于我使用嵌入式 tomcat,如何为我的 Web 应用程序配置默认会话超时?由于某种原因似乎默认为 30 分钟?我想设置为一周左右。
  2. 如果我从 eclipse 中启动应用程序,如何设置 autodeploy = true 以便每次修改 java 代码时都不必编译并重新启动应用程序?
  3. 有没有办法设置我的 web.xml 和 server.xml?
  4. 如何运行 apache tomcat 管理器?

网上的文档不是很清楚。你能帮忙吗?

提前致谢.. 基兰

I've been trying to modify embedded tomcat configuration for my heroku app. I've installed heroku app using the wiki link below that configures a simple embedded tomcat.

http://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat

The source code is here:

public static void main(String[] args) throws Exception {

    String webappDirLocation = "src/main/webapp/";
    Tomcat tomcat = new Tomcat();

    //The port that we should run on can be set into an environment variable
    //Look for that variable and default to 8080 if it isn't there.
    String webPort = System.getenv("PORT");
    if(webPort == null || webPort.isEmpty()) {
        webPort = "8080";
    }

    tomcat.setPort(Integer.valueOf(webPort));

    tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
    System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

    tomcat.start();
    tomcat.getServer().await();  

}

Questions:

  1. Since I'm using embedded tomcat, how do I configure default session timeout for my web application? It seems to default to 30 min for some reason? I want to set to something like one week.
  2. If I launch the application from within eclipse, how do I set autodeploy = true so that I don't have to compile and restart my application every time I modify java code?
  3. is there way to set my web.xml and server.xml?
  4. how do I run apache tomcat manager?

The documentation on the internet is not very clear. Can you please help?

Thanks in advance..
Kiran

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-12-28 22:12:35

使用 Context.setSessionTimeout(int)。 Java 文档此处。这是会话超时设置为 30 天的同一个 Main 类:

package launch;
import java.io.File;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.Context;


public class Main {

    public static void main(String[] args) throws Exception {

        String webappDirLocation = "src/main/webapp/";
        Tomcat tomcat = new Tomcat();

        //The port that we should run on can be set into an environment variable
        //Look for that variable and default to 8080 if it isn't there.
        String webPort = System.getenv("PORT");
        if(webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }

        tomcat.setPort(Integer.valueOf(webPort));

        Context ctx = tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        ctx.setSessionTimeout(2592000);
        System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

        tomcat.start();
        tomcat.getServer().await();  
    }
}

请注意 Context ctx = ...ctx.setSessionTimeout(...)

至于Tomcat Manager,当您以这种方式将Tomcat嵌入到您的应用程序中时,您将无法使用它。我很好奇您想使用 Tomcat Manager 做什么?

您通常在 server.xml 中执行的任何操作都可以通过嵌入 API 执行。嵌入的重点在于您可以通过编程方式配置所有内容。

您仍然可以像平常一样设置自己的 web.xml。只需将其添加到您作为 webappDirLocation 传入的目录下的 WEB-INF 目录中即可。但同样,我很好奇您想在 web.xml 中放入什么?因为您“拥有”主应用程序循环,所以您可以从主方法中设置所需的任何配置。我强烈建议您在主循环中初始化所需的所有内容,并读取特定于环境的任何内容(例如 JDBC url)的操作系统环境变量。

最后,对于 Eclipse,您不再需要热部署,因为您没有使用容器部署模型。您只需使用“调试为...”从 Eclipse 内部运行您的应用程序,Eclipse 将在您更改代码时自动编译并重新加载代码。它与热部署并不完全相同。例如,它不会热重载具有新方法签名的类。但与使用容器相比,循环整个应用程序要快得多,因此总的来说,我发现它的效率更高。

Use Context.setSessionTimeout(int). Java docs here. Here's the same Main class with a session timeout set to 30 days:

package launch;
import java.io.File;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.Context;


public class Main {

    public static void main(String[] args) throws Exception {

        String webappDirLocation = "src/main/webapp/";
        Tomcat tomcat = new Tomcat();

        //The port that we should run on can be set into an environment variable
        //Look for that variable and default to 8080 if it isn't there.
        String webPort = System.getenv("PORT");
        if(webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }

        tomcat.setPort(Integer.valueOf(webPort));

        Context ctx = tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        ctx.setSessionTimeout(2592000);
        System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

        tomcat.start();
        tomcat.getServer().await();  
    }
}

Notice the Context ctx = ... and ctx.setSessionTimeout(...).

As for Tomcat Manager, you can't use it when you embed Tomcat in your application this way. I am curious what you would like to use Tomcat Manager for?

Anything you would normally do from server.xml you can do via the embed API. The whole point of embedding is that you configure everything programmatically.

You can still set up your own web.xml as you normally would. Just add it in a WEB-INF directory under the directory you pass in as webappDirLocation. But again, I am curious what you would want to put in web.xml? Because you "own" the main application loop, you can set up any configuration you need from your main method. I highly recommend a practice of initializing everything you need in the main loop and reading OS environment variables for anything that is environment specific (e.g. a JDBC url).

Finally, as for Eclipse, you don't need hot deploy anymore because you are not using a container deployment model. You can simply run your application from inside Eclipse with "Debug as..." and Eclipse will auto compile and reload code as you change it. It's not exactly similar to hot deploy. For example, it won't hot reload classes with new method signatures. But it's much faster to cycle the whole app compared to when using a container, so overall, I find it much more productive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文