如何在Tomcat中为单个Web应用程序设置时区? [英] How do I set the timezone in Tomcat for a single web app?

查看:337
本文介绍了如何在Tomcat中为单个Web应用程序设置时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Tomcat中为单个网络应用设置时区的最佳方法是什么?我看到了更改Tomcat的命令行参数或环境变量的选项,但是有一种方法来设置它是自包含在WAR文件中,而不依赖于任何Tomcat配置?

What is the best way to set the time zone in Tomcat for a single web app? I've seen options for changing the command-line parameters or environment variables for Tomcat, but is there a way to set it that is self-contained in the WAR file and not dependent on any Tomcat configuration?

编辑:要重新强调,我正在寻找一个可以包含在WAR文件中的解决方案,而不依赖于Tomcat配置。换句话说,一个Web应用程序可以配置为与运行在同一个Tomcat实例中的其他应用程序具有不同的时区。

to reemphasize, I'm looking for a solution that can be contained within a WAR file, not dependent on Tomcat configuration. To put it another way, can one web app be configured to have a different time zone than other apps running in the same Tomcat instance?

推荐答案

我发现的唯一方法是设置过滤器并更改过滤器中的时区,

The only way I found is to setup a filter and change the timezone in the filter,

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    TimeZone savedZone = TimeZone.getDefault();
    TimeZone.setDefault(webappZone);
    chain.doFilter(request, response);
    TimeZone.setDefault(savedZone);
}

setDefault()更改线程的区域。因此,在过滤器内的线程中运行的所有内容都将有不同的默认时区。我们必须改变它,因为线程是由其他应用程序共享。你还需要对你的 init() destroy()方法和你可能开始的任何其他线程

The setDefault() changes the zone for the thread. So everything running in the thread inside the filter will have a different default timezone. We have to change it back because the thread is shared by other apps. You also need to do the same for your init(), destroy() methods and any other thread you might start in your application.

我不得不这样做,因为第三方库假设默认时区,我们没有源代码。这是一个烂摊子,因为这更改日志时区,但我们不想登录不同的时间。处理此问题的正确方法是使用暴露给最终用户的任何时间值中的特定时区。

I had to do this because a third-party library assumes default timezone and we don't have source code. It was a mess because this changes log timezone but we don't want log in different times. The correct way to handle this is to use a specific timezone in any time value exposed to end users.

这篇关于如何在Tomcat中为单个Web应用程序设置时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆