我们可以在 Servlet 中编写参数构造函数吗?如果是,你怎么打电话?
最佳答案
Can we write an argument constructor in a Servlet?
是的,你可以,但它没有用,因为 servlet 容器不会调用它。
正确的做法是使用 init()
方法:
@Override
public void init() throws ServletException {
String foo = getInitParameter("foo");
String bar = getServletContext().getInitParameter("bar");
// ...
}
在本例中, getInitParameter("foo")
返回 <init-param>
的值具体的<servlet>
进入 web.xml
, 和 getServletContext().getInitParameter("bar")
返回独立 <context-param>
的值在 web.xml
.
关于java - 如何在启动期间使用参数初始化 Servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1276082/