I'm getting the non-static method error.. this is for a servlet..
I'm trying to use getMimeType() method, which is a method of
ServletContext; I have read Roedy's page..
http://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF
but:
ServletContext is an interface, which means you can't create an object
from it..
(come to think of it, how come we can use Interface HttpServletRequest
in servlets without saying "implements HttpServletRequest"?)
i.e.,
public class myServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
aren't we instantiating HttpServletRequest here? which is an interface..
thank you very much..
Frances
Thomas Fritsch - 16 Feb 2006 22:59 GMT
> I'm getting the non-static method error.. this is for a servlet..
> I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 5 lines]
> ServletContext is an interface, which means you can't create an object
> from it..
Yes, you can't *create* an instance of ServletContext.
But you *get* an instance, by using appropriate get...() methods.
The Servlet interface has the method
public ServletConfig getServletConfig()
and the ServletConfig interface in turn has the method
public ServletContext getServletContext()
Hence, within your servlet you can do
ServletContext context = this.getServletConfig().getServletContext();
String mimeType = context.getMimeType(....);
You see: it's just an exercise in looking through the API docs ;-)
> (come to think of it, how come we can use Interface HttpServletRequest in
> servlets without saying "implements HttpServletRequest"?)
[quoted text clipped - 7 lines]
> aren't we instantiating HttpServletRequest here? which is an interface..
> thank you very much..
No, you are not. Instead the servlet container has instantiated the
HttpServletRequest somehow (you don't know how, and you don't care). Then
the servlet container called your servlet's doGet(...) method, passing the
HttpServletRequest to you.

Signature
"TFritsch$t-online:de".replace(':','.').replace('$','@')
Frances - 17 Feb 2006 03:10 GMT
>>I'm getting the non-static method error.. this is for a servlet..
>>I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 16 lines]
> String mimeType = context.getMimeType(....);
> You see: it's just an exercise in looking through the API docs ;-)
thank you.. I did look in docs... was looking for something like
ServletContext.getInstance()... (I guess from now when looking to
"instantiate" an interface will look for any get<InterfaceName>() method
throughout API..) thanks again.. Frances
trippy - 16 Feb 2006 23:27 GMT
> I'm getting the non-static method error.. this is for a servlet..
> I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 5 lines]
> ServletContext is an interface, which means you can't create an object
> from it..
ServletContext context = getServletContext();
> (come to think of it, how come we can use Interface HttpServletRequest
> in servlets without saying "implements HttpServletRequest"?)
[quoted text clipped - 9 lines]
>
> Frances

Signature
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "I Am The Law" -- Anthrax
"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."
-- Robert Redford "Spy Game"
trippy - 16 Feb 2006 23:53 GMT
> > I'm getting the non-static method error.. this is for a servlet..
> > I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 7 lines]
>
> ServletContext context = getServletContext();
It won't work if you don't use.
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
in the init() method first in some related servlet first.
Sorry, guess I should have mentioned that. My bad.
> > (come to think of it, how come we can use Interface HttpServletRequest
> > in servlets without saying "implements HttpServletRequest"?)
[quoted text clipped - 9 lines]
> >
> > Frances

Signature
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "I Am The Law" -- Anthrax
"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."
-- Robert Redford "Spy Game"
Frances - 17 Feb 2006 03:11 GMT
>>>I'm getting the non-static method error.. this is for a servlet..
>>>I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 12 lines]
> ServletConfig config = getServletConfig();
> ServletContext context = config.getServletContext();
yes noticed, since ServletContext.getServletContext() doesn't work..
and was confused at first b/c looked in API and found a bunch of
getServletContext() methods, but none of them is of ServletContext
interface..
thank you very much for your response, works now...
> in the init() method first in some related servlet first.
>
> Sorry, guess I should have mentioned that. My bad.
trippy - 17 Feb 2006 05:01 GMT
> >>>I'm getting the non-static method error.. this is for a servlet..
> >>>I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 19 lines]
>
> thank you very much for your response, works now...
NP. Glad it helped.
> > in the init() method first in some related servlet first.
> >
> > Sorry, guess I should have mentioned that. My bad.

Signature
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "I Am The Law" -- Anthrax
"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."
-- Robert Redford "Spy Game"
Frances - 17 Feb 2006 15:23 GMT
>>>>>I'm getting the non-static method error.. this is for a servlet..
>>>>>I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 22 lines]
>
> NP. Glad it helped.
thanks for all yr help, trippy.. I had also tried this,
public class myServlet extends HttpServlet implements ServletContext {
and it wouldn't compile.. why is this.. thanks again.. Frances
trippy - 18 Feb 2006 00:23 GMT
> >>>>>I'm getting the non-static method error.. this is for a servlet..
> >>>>>I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 28 lines]
>
> and it wouldn't compile.. why is this.. thanks again.. Frances
Context isn't a reserved word like out, or request or response or
session.
You could have this for instance:
ServletContext fred = config.getServletContext()
and fred would work where context would normally be.(I do use context
though, because it's a hell of a lot easier to remember than anything
else.) The return type for a config.getServletContext() is a
ServletContext object.
That's why you can use the interface without implementing it because you
already get that return type back from the method call.
A similar thing would be like using
Enumeration en = somePropertiesObject.keys();

Signature
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "I Am The Law" -- Anthrax
"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."
-- Robert Redford "Spy Game"
Roedy Green - 17 Feb 2006 04:30 GMT
>public class myServlet extends HttpServlet {
> protected void doGet(HttpServletRequest request, HttpServletResponse
>response)
>
>aren't we instantiating HttpServletRequest here? which is an interface..
> thank you very much..
see http://mindprod.com/jgloss/interface.html
http://mindprod.com/jgloss/interfacevsabstract.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Nigel Wade - 17 Feb 2006 11:40 GMT
> I'm getting the non-static method error.. this is for a servlet..
> I'm trying to use getMimeType() method, which is a method of
[quoted text clipped - 5 lines]
> ServletContext is an interface, which means you can't create an object
> from it..
Right, you have to implement it in a class, or have an object who's class has
implemented it. To use ServletContext.getMimeType() you need an object who's
class has implemented the interface. For a servlet you can get this from the
GenericServlet.getServletContext() method, ie. this.getServletContext() will
return an object which has implemented the ServletContext interface.
> (come to think of it, how come we can use Interface HttpServletRequest
> in servlets without saying "implements HttpServletRequest"?)
Some class implements it, and you get an object of that class.
> i.e.,
>
[quoted text clipped - 4 lines]
> aren't we instantiating HttpServletRequest here? which is an interface..
> thank you very much..
No. The class of which "request" is an instance has implemented it. You don't
need to know what class "request" is an instance of, all you need to know is
that this class has implemented the HttpServletRequest interface. this allows
you to invoke methods from that interface on the request object. It means that
the object which is passed as the first parameter to the doGet() method can be
of any class provided it implements the interface.
HttpServletRequest isn't being instantiated, but a class which implements it has
been.

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555