2

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

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.xml i have added context parameters as follows,

<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.

Saveendra Ekanayake
  • 3,153
  • 6
  • 34
  • 44

5 Answers5

9

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.

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

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

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 ServletContext using the method session.getServletContext().

karim mohsen
  • 2,164
  • 1
  • 15
  • 19
1

From javadoc:

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

So basically it is the same.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
1

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

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

@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 config variable of GenericServlet class to get ServletContext object.

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

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

rajuGT
  • 6,224
  • 2
  • 26
  • 44
0

Both methods provide access to the same object.

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

Andres
  • 10,561
  • 4
  • 45
  • 63
  • This isn't a question about Java -- it's a question about the Servlet API. The following methods are not the same: `request.getServletContext()` -- declared in HttpServletRequest, `request.getSession().getServletContext()` -- declared in HttpSession, and `this.getServletContext()` -- declared in GenericServlet. So whether they return the same object is a matter of what the API defines and what the implementation actually does. And no one has fully answered _that_ question yet. – AndrewF Dec 05 '15 at 04:59
0

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

samid hamza
  • 1
  • 1
  • 1