• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue trying to use getServletContext() in a JEE project

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an issue trying to use getServletContext() on the server-side. This is marked as an error (underlined in red) and the hover over error is "The method getServletContext() is undefined for the type MySQLConnection" and the quick fix is to create the mothod.

I am using some code that was generated by someone else for gwtJava. I am now trying to use this in a JEE project. Unfortunately I have not been able to get help from the person who originally created this code and I am not a programmer and am learning as I go.


The ScoutMain.generateE1 has:



I have looked at:

Different ways to get Servlet Context

Why getservletcontext isn't found?

And the getServletContext() documentation, which I found very confusing.

I think I may be missing an import; however, cannot find which one.

Kind regards,

Glyn
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show your class signature, the one starting "public class <your class name> etc etc"?
 
Saloon Keeper
Posts: 7582
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not missing an import, and you don't have to (actually, can't) implement that method. You seem to be calling it from a class that does not extend either GenericServlet or HttpServlet.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This fails Basic Java.

If you do not specify a base object or class for a method call, the method call is expected to be a member of the class that the method call logic is located in - or at least a superclass of that class.

Java is not like C/C++ where you have stand-alone functions. Java is fully object-oriented and every method must belong to a class.

That's where your error comes from. I can deduce that your sample code is in a class you created named "MySQLConnection" because the compiler cannot find a getServletContext method defined in your class definition.

The getServletContext() JEE method is a member method of class javax.servlet.http.GenericServlet and its descendents and one or two other servlet-related classes such as javax.servlet.http.HttpSession. So if you want the ServletContext, you either need to obtain it from the Servlet that is (presumably) calling MySQLConnection methods or from the HttpSession object for your webapp.
 
Glyndwr Bartlett
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim Moores,

Yes, thank you. By removing getServletContext() from the ScoutMain.generateE1 and all reference to it in the called program it now works. If I can ever catch the person who produced the GWT version I will ask them why it was in there.

Kind regards,

Glyn

Hi Tim Holloway,

I understand the first three paragraphs of your reply; however, struggling with the last. I obviously need some education in this area; which I will see to.

Kind regards,

Glyn
reply
    Bookmark Topic Watch Topic
  • New Topic