This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Greenhorn
Posts: 27
  • 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
 
Master Rancher
Posts: 3887
45
  • 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: 5235
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: 20353
111
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
Greenhorn
Posts: 27
  • 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
 
crispy bacon. crispy tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!