Java 获取 Servlet 上下文的不同方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35837285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different ways to get Servlet Context
提问by McSonk
Could anybody explain me what is the difference between this ways of getting the ServletContext
of an HttpServlet
?
有人能解释一下这种获取ServletContext
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?
性能或上下文本身有什么区别吗?如果是这样,哪种方法最好?有没有其他方法可以检索上下文?
采纳答案by BalusC
There's one more.
还有一个。
request.getServletContext();
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.
从技术上讲,性能没有区别,只有request.getSession()
在尚未创建的情况下才会隐式创建 HTTP 会话对象。因此,如果这还没有完成,那么如果尚未创建会话,则通过会话获取 servlet 上下文可能需要多几纳秒的时间。
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.
返回的上下文也没有区别。这些方法都是为了方便起见,获取上下文的方法取决于上下文 ;) 您当前所在的位置。
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.
如果你坐在一个方法调用由servlet的service()
(如doGet()
,doPost()
等),那么就使用继承的getServletContext()
方法。其他方式只会在源代码中不必要地添加更多字符。
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
ServletContext context = getServletContext();
// ...
}
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
.
如果您正在使用 servlet 的init(ServletConfig)
方法,那么getServletContext()
只要您还没有调用super.init(config)
. 你需要从ServletConfig
.
@Override
public void init(ServletConfig config) {
ServletContext context = config.getServletContext();
// ...
}
But much better is to override init()
instead. In a decent servlet you usually never need to override init(ServletConfig)
.
但更好的是覆盖init()
。在一个体面的 servlet 中,您通常不需要覆盖init(ServletConfig)
.
@Override
public void init() {
ServletContext context = getServletContext();
// ...
}
If you're not sitting in a servlet but in e.g. a filterwhich lacks the inherited getServletContext()
method and you only have ServletRequest
at hands, then you could grab it from there.
如果您不是坐在 servlet 中,而是在例如缺少继承方法的过滤器中,getServletContext()
并且您只有ServletRequest
手头,那么您可以从那里获取它。
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
ServletContext context = request.getServletContext();
// ...
}
Note that this is new since Servlet 3.0. Previously, you'd have to grab it from the session.
请注意,这是自 Servlet 3.0 以来的新功能。以前,您必须从会话中获取它。
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
ServletContext context = request.getSession().getServletContext();
// ...
}
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!).
但是,如果您担心不必要的会话创建,这并不好。因此引入了ServletRequest#getServletContext()
- 尽管您也可以简单地从中提取它FilterConfig
(嘿,还有另一种方法!)。
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();
// ...
}
And then there are HTTP session listenerswhere you could listen on a.o. session destroy. There's no other way to obtain the servlet context than via HttpSession#getServletContext()
.
然后还有HTTP 会话侦听器,您可以在其中侦听 ao 会话销毁。除了 via 之外,没有其他方法可以获取 servlet 上下文HttpSession#getServletContext()
。
@Override
public void sessionDestroyed(HttpSessionEvent event) {
ServletContext context = event.getSession().getServletContext();
// ...
}
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.
在这里您无需担心不必要的会话创建,因为它在那时已经创建了很长时间。请注意,没有ServletRequest
任何地方,因为在服务器端会话超时期间不一定有活动 HTTP 请求的手段。
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#getContext()
返回ServletContext
部署到同一服务器的不同 Web 应用程序的(仅当服务器配置为在目标 Web 应用程序上启用跨上下文访问时才有效)。
ServletContext otherContext = context.getContext("/otherContextPath");
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.
但这已经需要电流ServletContext
开始,为此您现在应该已经知道使用哪种方式来获取它。