获取 Servlet Context 的不同方式 [英] Different ways to get Servlet Context

查看:22
本文介绍了获取 Servlet Context 的不同方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下获取 HttpServletServletContext 的这种方式有什么区别?

Could anybody explain me what is the difference between this ways of getting the ServletContext of an HttpServlet?

doGet( HttpServletRequest request, ... ){
    getServletConfig( ).getServletContext( );
    request.getSession( ).getServletContext( );
    getServletContext( );
}

性能或上下文本身有什么不同吗?如果是这样,哪种方法最好?还有其他检索上下文的方法吗?

Is there any difference in performance or in the context itself? If so, which is the best way? Are there any other way of retrieving the context?

推荐答案

还有一个.

request.getServletContext();

技术上没有性能差异,只有 request.getSession() 会隐式创建 HTTP 会话对象(如果尚未创建).因此,如果这还没有完成,那么如果尚未创建会话,则通过会话获取 servlet 上下文可能需要几纳秒的时间.

There's technically no difference in performance, only the request.getSession() will implicitly create the HTTP session object if not created yet. So if this is not done yet, then grabbing the servlet context via the session may take a few nanoseconds longer if the session isn't created yet.

返回的上下文也没有区别.这些方法都是为了方便起见,获取上下文的方法取决于上下文 ;) 您当前所在的位置.

There's also no difference in the returned context. Those methods are all just for convenience and which method to obtain the context depends on the context ;) you're currently sitting in.

如果您正在使用由 servlet 的 service()(如doGet()doPost() 等),然后只使用继承的 getServletContext() 方法.其他方式只会在源代码中不必要地添加更多字符.

If you're sitting in a method invoked by servlet's service() (such as doGet(), doPost(), etc), then just use the inherited getServletContext() method. Other ways only unnecessarily add more characters to the source code.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    ServletContext context = getServletContext();
    // ...
}

如果您坐在 servlet 的 init(ServletConfig) 方法,那么继承的 getServletContext() 还不可用,只要你还没有调用 super.init(config).您需要从 获取它ServletConfig.

If you're sitting in servlet's init(ServletConfig) method, then the inherited getServletContext() isn't available yet as long as you haven't called super.init(config). You'd need to grab it from ServletConfig.

@Override
public void init(ServletConfig config) {
    ServletContext context = config.getServletContext();
    // ...
}

但更好的是覆盖 init() 代替.在一个体面的 servlet 中,您通常不需要覆盖 init(ServletConfig).

@Override
public void init() {
    ServletContext context = getServletContext();
    // ...
}

如果您不是坐在 servlet 中,而是在例如一个 filter 缺少继承的 getServletContext() 方法,而您只有 ServletRequest 在手,然后你可以从那里获取它.

If you're not sitting in a servlet but in e.g. a filter which lacks the inherited getServletContext() method and you only have ServletRequest at hands, then you could grab it from there.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    ServletContext context = request.getServletContext();
    // ...
}

请注意,这是自 Servlet 3.0 以来的新功能.以前,您必须从会话中获取它.

Note that this is new since Servlet 3.0. Previously, you'd have to grab it from the session.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    ServletContext context = request.getSession().getServletContext();
    // ...
}

然而,如果您担心不必要的会话创建,这并不好.因此引入了 ServletRequest#getServletContext() —虽然你也可以简单地从 FilterConfig(嘿,还有另一种方法!).

This is however not nice if you worry about unnecessary session creation. Hence the introduction of ServletRequest#getServletContext() — although you could also simply extract it from FilterConfig (hey, there's yet another way!).

private FilterConfig config;

@Override
public void init(FilterConfig config) {
    this.config = config;
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    ServletContext context = config.getServletContext();
    // ...
}

然后是 HTTP 会话监听器 哪里可以收听ao会话销毁.除了通过 HttpSession#getServletContext().

And then there are HTTP session listeners where you could listen on a.o. session destroy. There's no other way to obtain the servlet context than via HttpSession#getServletContext().

@Override
public void sessionDestroyed(HttpSessionEvent event) {
    ServletContext context = event.getSession().getServletContext();
    // ...
}

在这里您无需担心不必要的会话创建,因为它在那时已经创建了很长时间.请注意,任何地方都没有 ServletRequest,因为在服务器端会话超时期间不一定有活动 HTTP 请求的手段.

Here you don't need to worry about unnecessary session creation because it's at that point already created for long beforehand. Do note that there's no ServletRequest anywhere as there's not necessarily means of an active HTTP request during server side session timeout.

最后,还有 ServletContext#getContext() 返回部署到同一服务器的不同 Web 应用程序的 ServletContext(仅当服务器配置为启用交叉目标 webapp 上的上下文访问).

As last, there's also ServletContext#getContext() which returns the ServletContext of a different web application deployed to same server (this works only if the server is configured to enable cross context access on the target webapp).

ServletContext otherContext = context.getContext("/otherContextPath");

但是这已经需要当前的 ServletContext 开始,为此您现在应该已经知道使用哪种方式来获取它.

But this already requires the current ServletContext to start with, for which you should by now already know which way to use to obtain it.

这篇关于获取 Servlet Context 的不同方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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