• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Issue trying to use getServletContext() in a JEE project  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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: 3983
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show your class signature, the one starting "public class <your class name> etc etc"?
 
Saloon Keeper
Posts: 5328
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Bartender
Posts: 20562
120
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 36
  • Mark post as helpful
  • send pies
  • 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
 
Whatever you say buddy! And I believe this tiny ad too:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!