Java request.getServletContext() 和 getServletContext() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32610015/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:51:02  来源:igfitidea点击:

Difference between request.getServletContext() and getServletContext()

javajakarta-ee

提问by Saveendra Ekanayake

I have SampleServletclass, within that I have override the doGet()method as follows

我有SampleServlet课程,其中我已经覆盖了doGet()如下方法

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String name = request.getParameter("name");

    String userid = (String)request.getServletContext().getInitParameter("userid");

    out.print("Name = " + name + "<br>");

    out.print("User id= " + userid+ "<br>");
}

Within my Web.xmli have added context parameters as follows,

在我的内部,我Web.xml添加了如下上下文参数,

<context-param>
    <param-name>userid</param-name>
    <param-value>ABC12345</param-value>
</context-param>

I used request.getServletContext().getInitParameter("userid");statement to access that parameter.request.getServletContext().getInitParameter("userid");Its work fine. However is there any difference between getServletContext().getInitParameter("userid");and request.getServletContext().getInitParameter("userid");Both give me same output but I don't have proper idea about these two.

我使用request.getServletContext().getInitParameter("userid");语句来访问该参数。request.getServletContext().getInitParameter("userid");它的工作正常。但是getServletContext().getInitParameter("userid");request.getServletContext().getInitParameter("userid");两者之间有什么区别,两者都给了我相同的输出,但我对这两者没有正确的了解。

采纳答案by karim mohsen

getServletContext()you can call directly is only when your code is in a class that extends HttpServlet. That is because HttpServlet base class has this method defined.

getServletContext()只有当您的代码位于扩展 HttpServlet 的类中时,您才能直接调用。那是因为 HttpServlet 基类定义了这个方法。

ServletContext returned by request.getSession().getServletContext()is same as getServletContext().HttpSessioncontains a reference to the ServletContext that this session belongs to.

返回的 ServletContextrequest.getSession().getServletContext()getServletContext().HttpSession相同,包含对这个会话所属的 ServletContext 的引用。

As long as your code is in the servlet class, you can use anything as both can be called.

只要您的代码在 servlet 类中,您就可以使用任何东西,因为两者都可以被调用。

If you have a custom class that doesn't extend servlet and you need to pass the session object to work on it in that custom class.As you have a reference to the session, you can get access to the ServletContextusing the method session.getServletContext().

如果您有一个不扩展 servlet 的自定义类,并且您需要传递会话对象以在该自定义类中处理它。因为您有对会话的引用,您可以访问ServletContextusing 方法session.getServletContext()

回答by Davide Lorenzo MARINO

From javadoc:

javadoc

Gets the servlet context to which this ServletRequest was last dispatched.

获取上次将此 ServletRequest 分派到的 servlet 上下文。

So basically it is the same.

所以基本上是一样的。

回答by Andres

Both methods provide access to the same object.

这两种方法都提供对同一对象的访问。

When you write getServletContext().getInitParameter("userid");there's an implicit thison the left of the method. That means you're using the method from GenericServletclass.

当您编写时,方法的左侧getServletContext().getInitParameter("userid");有一个隐式this。这意味着您正在使用GenericServlet类中的方法。

回答by rajuGT

Try calling getInitParameter("userId") directly without request.getServletConfig()

尝试在没有 request.getServletConfig() 的情况下直接调用 getInitParameter("userId")

It works. The reason is Inheritence. Your servlet class is subclass of javax.servlet.GenericServletand its public methods are accessible to your class also. It has the following method

有用。原因是继承。您的 servlet 类是 javax.servlet 的子类。您的类也可以访问GenericServlet及其公共方法。它有以下方法

@Override
public String getInitParameter(String name) {
    return getServletConfig().getInitParameter(name);
}

And request object also has accessor to get servletConfig and by using it, we can access getInitParameter method. Its ultimately accessing private transient ServletConfig configvariable of GenericServlet class to get ServletContext object.

并且请求对象也有获取servletConfig 的访问器,通过使用它,我们可以访问getInitParameter 方法。它最终访问private transient ServletConfig configGenericServlet 类的变量以获取 ServletContext 对象。

That's the reason you are able to access the getServletContext().getInitParameter("userid"); without using "request" parameter.

这就是您能够访问 getServletContext().getInitParameter("userid"); 的原因。不使用“请求”参数。

PS: Code is taken from apache-tomcat source. Go through it, you will have more clarity.

PS:代码取自 apache-tomcat 源码。通过它,你会更清楚。

回答by samid hamza

Basically it's the same. But if you use request.getSession().getServletContext() you will create a session when you actually don't need it.

基本上是一样的。但是如果你使用 request.getSession().getServletContext() 你会在你实际上不需要它时创建一个会话。