jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Obtain ServletContext from Request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Obtain ServletContext from Request" Watch "Obtain ServletContext from Request" New topic
Author

Obtain ServletContext from Request

Jeff Black
Greenhorn

Joined: Aug 02, 2002
Posts: 14
From a bean that I only pass HttpServletRequest into, how can I obtain a reference to it's corresponding ServletContext?
I want the bean to be able to get at the ServletContext.getInitialParameter() method.

Jeff
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
ServletContext sc = req.getSession().getServletContext();


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jeff Black
Greenhorn

Joined: Aug 02, 2002
Posts: 14
No such method session.getServletContext() for servlet 2.2 API. I've already scanned the javax.servlet... API and there is no direct route to get the servletContext. Surely there must be another way?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

I posted a similar question a while back. The getServletContext() method on HttpSession is new in Servlet 2.3. Apparently under 2.2 there's no way to get the context given just a request, and we must resort to passing it around with the request, or adding it to the request as an attribute before passing the request around. Ick.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
wood burning stoves
 
subject: Obtain ServletContext from Request
 
Similar Threads
clarification in the parameter of response.sendRedirect and request.getRequestDispatc
Replacement of deprecated servlet method
How to get the ip address, port and context path from a Servlet's init method
how-to call some servlet or bean when war is loaded
Generic Servlet & ServletContext