ServletConfig

C

cameron

I am very new to Java and I work with C# at work, (so please
excuse my C#/M$ references), I need a reference to the
ServletConfig in some of my classes that get instanciated in
a servlet, but the idea of passing the ServletConfig into
each object seems silly. In C# I can do something like:

HttpServerContext.Current

To get a current reference. Is there an equivelent
class/reference for the ServletConfig object or something
that will do/have the equivelent methods and attributes?

Thanks

-Cam
 
T

Tony Morris

cameron said:
I am very new to Java and I work with C# at work, (so please
excuse my C#/M$ references), I need a reference to the
ServletConfig in some of my classes that get instanciated in
a servlet, but the idea of passing the ServletConfig into
each object seems silly. In C# I can do something like:

HttpServerContext.Current

To get a current reference. Is there an equivelent
class/reference for the ServletConfig object or something
that will do/have the equivelent methods and attributes?

Thanks

-Cam

There is a ServletConfig object which is passed to the init method of a
servlet.
However, your analogy to C# seems to suggest that you are after the
ServletContext.
You obtain this by calling getServletContext() on your instance of
GenericServlet.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
C

cameron

Tony said:
There is a ServletConfig object which is passed to the init method of a
servlet.
However, your analogy to C# seems to suggest that you are after the
ServletContext.
You obtain this by calling getServletContext() on your instance of
GenericServlet.

But I am in a class that was instanciated in a servlet, so
while the servlet has an instance of GenericServlet, the
class I am in does not have one and it doesn't seem happy
when I attempt to make one.

-Cam
 
A

Anton Spaans

cameron said:
But I am in a class that was instanciated in a servlet, so
while the servlet has an instance of GenericServlet, the
class I am in does not have one and it doesn't seem happy
when I attempt to make one.

-Cam

It is not possible then. The application can have multiple Servlet-instance
running at the same time. So a 'static' variable like
'HttpServerContext.Current' is not possible, since there can be more than
one. But if Current() were to be a method, it could examine the environment
and get the current request's context.

A way around would be to get hold of a HttpSession object. Through this
object, you can call getServletContext(). If you don't have access to a
HttpSession object, you could (mis-)use the HttpServletRequest instance to
create one (call getSession(true) on it):

request.getSession(true).getServletContext()

If you don't have access to any of these instances, you're out of luck. The
class you're dealing with should be redesigned to be properly initialized
with the current servlet-context.

-- Anton Spaans.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top