简体   繁体   English

什么时候使用Servlet初始化参数?

[英]When would you use Servlet init params?

I was reading about Initialization Parameters for Servlets on this blog post by Jenkov. 我在Jenkov撰写的此博客文章中阅读了有关Servlet的初始化参数的信息。

It made me wonder though, when would I ever need to initialize variables this way? 但是,这使我感到奇怪,我何时需要以这种方式初始化变量? If I wanted to predefine certain values, why not just do it with fields in the Java code? 如果我想预定义某些值,为什么不只使用Java代码中的字段呢?

It could be useful if your servlet was packaged in a jar for use by another user, who could then set the input parameters to their own specification in their own servlet definition. 如果将servlet打包在jar中供其他用户使用,则该用户可能会很有用,然后该用户可以在自己的servlet定义中将输入参数设置为其自己的规范。 If you hard coded it, they could not otherwise 'configure' your servlet. 如果您对其进行了硬编码,则他们将无法“配置”您的servlet。

因为如果您需要更改这些变量,则一个需要编辑web.xml并重新启动servlet,而另一个(您的建议)则需要重新编译应用程序。

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

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