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

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

问题描述

有人能解释一下这种获取 HttpServlet ServletContext 之间的区别是什么?

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()就不可用了code> super.init(配置)。您需要从 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();
    // ...
}

但更好的是覆盖< a href =https://docs.oracle.com/javaee/7/api/javax/servlet/GenericServlet.html#init-- =noreferrer> init() 。在一个体面的servlet中,你通常永远不需要覆盖 init(ServletConfig)

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

如果你没有坐在一个servlet,但在例如过滤,缺少继承的 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会话毁灭。没有其他方法可以获得servlet上下文而不是通过 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天全站免登陆