什么是 getServletContextLocal() 和 getServletContextProvider()
在我阅读的教程中,我遇到了 getServletContext() 但这两个是做什么用的?
这就是该行的外观:
ServletContext sc = getServletContextProvider().getServletContextLocal();
这与以下有何不同:
ServletContext sc = getServletContext();
In the tutorials I read I came across getServletContext() but what are these 2 for ?
This is how the line looks:
ServletContext sc = getServletContextProvider().getServletContextLocal();
How is this different from:
ServletContext sc = getServletContext();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的答案特定于可能实现 ServletContextProvider 接口的类。为了正确回答这个问题,您需要提供声明
ServletContextProvider
的包,或者更有用的是,提供包含getServletContextProvider()
和的类或接口>getServletContext()
方法(声明的或抽象的)。对我们来说更容易的是,提供您引用的教程的链接或一些其他上下文(没有双关语)。互联网上对
getServletContextLocal
方法的唯一引用(Google 搜索后)要么是这个问题,要么是这个问题的副本。假设 getServletContextProvider() 返回一个 ServletContextProvider 类或接口,有多个具有该名称的接口和类;我将详细介绍我找到的每一个:
getServletContextLocal()
方法。getServletContextLocal()
方法;该接口的实现者,LiferayServletContextProvider
没有getServletContextLocal()
方法,并且扩展了Object。getServletContextLocal()
方法,也没有它实现了ResourceProvider
接口,并扩展了 Object。getServletContextLocal()
方法,该链接上列出的 4 个实现类也没有。getServletContextLocal()
方法,并且扩展了 Object。getServletContextLocal()
方法。结论:您输入的内容可能是您阅读的教程中的拼写错误,可能引用了最常见的可用接口,
org.apache.portals.bridges.common.ServletContextProvider
,它有一个getServletContext(GenericPortlet portlet)
方法。本教程可能打算编写的是 getServletContextProvider().getServletContext(local) ,其中 local 变量是扩展 javax.portlet.GenericPortlet 的类。然后,getServletContext(local)
方法将返回与所述变量关联的javax.servlet.ServletContext
。它是否与getServletContext()
返回的javax.servlet.ServletContext
相同取决于GenericPortlet
变量的分配位置。帮自己一个忙,引用您所参考的教程的位置。
Your answer is specific to the class likely implementing a
ServletContextProvider
interface. In order to answer this properly, you need to provide the package whereServletContextProvider
is declared, or even more helpful, the class or interface which contains thegetServletContextProvider()
andgetServletContext()
methods (declared or abstract).Even easier for us, provide a link to the tutorial you reference, or some additional context (no pun intended). The only references to a
getServletContextLocal
method on the internet (after a Google search) are either this question, or copies of this question.Assuming
getServletContextProvider()
returns aServletContextProvider
class or interface, there are several interfaces and classes available with that name; I'll go through each one I found:getServletContextLocal()
method.getServletContextLocal()
method; Implementer of said interface,LiferayServletContextProvider
doesn't have thegetServletContextLocal()
method, and extends Object.getServletContextLocal()
method, nor does theResourceProvider
interface it implements, and it extends Object.getServletContextLocal()
method, nor do the 4 implementing classes listed on that link.getServletContextLocal()
method, and extends Object.getServletContextLocal()
method.Conclusion: What you've typed is likely a typo in the tutorial you read, probably referencing the most common interface available,
org.apache.portals.bridges.common.ServletContextProvider
, which has agetServletContext(GenericPortlet portlet)
method. What the tutorial likely intended to write isgetServletContextProvider().getServletContext(local)
where thelocal
variable is a class extendingjavax.portlet.GenericPortlet
. ThegetServletContext(local)
method would then return thejavax.servlet.ServletContext
which was associated with said variable. Whether it is the samejavax.servlet.ServletContext
as returned bygetServletContext()
depends on where theGenericPortlet
variable was assigned.Do yourself a favor and cite the location of the tutorial to which you're referring.