概要servlet应用程序,在其中存储我可以引用的配置设置



我有一个基本的servlet应用程序。

是否将配置相关信息存储在web.xml文件中?

在我的servlet中是否有一个事件,我应该将配置值保存到一个静态只读变量?

我现在有一个servlet,但是是否有另一个文件在全局级别开始生命周期?

就像在。net中一样,你有你的页面,但有一个global.asax.cs类在特定事件时触发,如:

application_startup
application_shutdown
application_beginRequest
application_endRequest

servlet有这个吗,还是每个servlet都有?

application_startup
application_shutdown

实现ServletContextListener

@WebListener
public class ApplicationListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Do your job here on application startup.
    }
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // Do your job here on application shutdown.
    }
}

您可以将应用程序范围的变量存储为ServletContext的属性。

event.getServletContext().setAttribute("foo" new Foo());

在servlet中可以通过继承的getServletContext()方法获得。

Foo foo = (Foo) getServletContext().getAttribute("foo");

在jsp中只使用EL。

${foo.someProperty}

application_beginRequest
application_endRequest

实现ServletRequestListener:

@WebListener
public class RequestListener implements ServletRequestListener {
    @Override
    public void requestInitialized(ServletRequestEvent event) {
        // Do your job here on request begin.
    }
    @Override
    public void requestDestroyed(ServletRequestEvent event) {
        // Do your job here on request end.
    }
}

您可以将请求范围变量存储为ServletRequest的属性。

event.getServletRequest().setAttribute("foo" new Foo());

可以通过传入的HttpServletRequest参数在servlet中使用。

Foo foo = (Foo) request.getAttribute("foo");

在jsp中只使用EL。

${foo.someProperty}

你甚至可以在一个类中实现这两个接口:

@WebListener
public class GlobalListener implements ServletContextListener, ServletRequestListener {
    // ...
}

或者,更常见的,如果你希望能够更全局地修改/控制请求,Filter:

@WebFilter(urlPatterns={"/some/*"})
public class SomeFilter implements Filter {
    @Override
    public void init(FilterConfig config) throws ServletException {
        // Do here your application startup job.
        // If you have any <init-param> in web.xml, then you could get them
        // here by config.getInitParameter("name") and assign it as field.
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // Do here your request/response preprocessing job.
        // Continue the request/response (if you haven't already forwarded/redirected the request/response beforehand).
        chain.doFilter(request, response);
        // Do here your request/response postprocessing job.
    }
    @Override
    public void destroy() {
        // Do here your application shutdown job.
        // If you have assigned any expensive resources as field of
        // this Filter class, then you could clean/close them here.
    }
}

参见https://stackoverflow.com/tags/servlet-filters/info。

Servlet API的所有其他侦听器都可以在javax.servlet包中找到接口。

application_startupapplication_shutdown请查看javax.servlet.ServletContextListener.

对于application_beginRequestapplication_endRequest,请查看javax.servlet.Filter.

都使你能够管理一个作用域(以映射或初始化参数的形式),你可以在其中放置你的配置和/或初始化/最终化组件,通过相应的级别事件。

servlet有一个加载顺序;它曾经是典型的有一个启动servlet执行初始化任务,在其他servlet之前加载。

现在这通常由启动侦听器处理。

http://download.oracle.com/docs/cd/B14099_19/web.1012/b14017/filters.htm i1000654

可以在初始参数中存储参数,但也有其他选项,如JNDI,属性文件等。

最新更新