16

My compiler is not able to find the HttpServletRequest getServletContext() method.

I am not doing anything too complicated:

public static void setMySortedSet(HttpServletRequest request, SortedSet<String> set) 
{
   setMySortedSet(request.getServletContext(), set);
}

Some troubleshooting I have tried:

  • Discovered the method was created in 2.3, so I included a JAR that reflects that (and have it in my Eclipse build path)
  • I include the JAR in my build.xml classpath.

When I using Eclipse the method is found but when I try to build the classes I see this:

compile:
[javac] Compiling 1 source files to C:\...\workspace\proj\build\WEB-INF\classes
[javac] C:\...\workspace\proj\src\main\Helper.java:26: cannot find symbol
[javac] symbol  : method getServletContext()
[javac] location: interface javax.servlet.http.HttpServletRequest
[javac]     return getURISet(request.getServletContext());
[javac]                       ^
[javac] Note: C:\...\workspace\proj\src\main\Helper.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error

Any ideas of what I could be missing? I appreciate any responses.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dave Brock
  • 287
  • 3
  • 5
  • 14

4 Answers4

28

The getServletContext() method is introduced in Servlet 3.0, not 2.3. But if you want to get the ServletContext then an alternative method to get it is:

ServletContext context = request.getSession().getServletContext();

if (username != "" & username != null ) {
    context.setAttribute("savedUserName", username);
}
writer.println("Context Parameter : " + (String)context.getAttribute("savedUserName"));

This way you can get the stored Request Parameter Value in different browser....

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Neha Velhal
  • 281
  • 1
  • 3
  • 2
  • 1
    What if you want to get the ServletContext in Servlet spec < 3.0 *without* starting a session? – Asaph Oct 30 '13 at 22:30
19

According to the Javadoc the ServletRequest#getServletContext() method is introduced in Servlet 3.0, not 2.3. You need to install and integrate a Servlet 3.0 compatible container such as Tomcat 7, Glassfish 3, etc in Eclipse and set the Target Runtime of your Dynamic Web Project to that container. When you do that properly, then you do not need to manually fiddle with build paths or build.xml at all, Eclipse will handle it for you automatically. You also do not need to download loose JAR files of an arbitrary servletcontainer of a different make/version and put it in your buildpath. It would only lead to future classpath and portability troubles.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for the response. I have discovered that I needed to not have the project as a Java Project so have created it as a Dynamic Project, set the runtime to Tomcat 7.0, and still have the same error. Is there anything else I should be aware of? – Dave Brock Oct 22 '11 at 17:23
  • Note that it has to be a dynamic **Web** project. Your project has to be created as Servlet 3.0 project (you can change this afterwards in *Project Facets* property). Only Eclipse Helios (3.6) and newer support this (otherwise you need to upgrade). You also need to ensure that your `web.xml` root declaration conforms Servlet 3.0 spec (you can just manually edit the XML). – BalusC Oct 22 '11 at 17:25
  • It is a Dynamic Web Project (sorry about the typo) and when going into _Project Facets_ the **Dynamic Web Module** is at v3.0. The web.xml reads: ``. – Dave Brock Oct 22 '11 at 17:32
  • And you do for sure **not** have any handpicked servletcontainer-specific libraries in your `/WEB-INF/lib` folder or in project's buildpath? See also the "See also" link in my answer. – BalusC Oct 22 '11 at 17:35
  • I checked your other page and ensured everything is up to the correct version and I don't have any other dependencies. So frustrating! – Dave Brock Oct 22 '11 at 17:46
  • How exactly are you compiling? Using Eclipse's builtin compiler and just typing/saving code in Eclipse without manually fiddling with ant or javac? Do you see any Eclipse error/warning in the source code view at the `request.getServletContext()` line? If not, then that part is perfectly fine. – BalusC Oct 22 '11 at 17:48
  • I'm compiling using Ant and this time, Eclipse does not understand the method in the source code view. – Dave Brock Oct 22 '11 at 17:52
  • You're manually fiddling with ant? Then your problem is in the ant side. Why don't you just use Eclipse's own compiler and its autogenerated build file? Did you edit/copy the old project's source files or did you build on a blank and unmodified default created dynamic web project? With what purpose exactly are you manually fiddling with ant? – BalusC Oct 22 '11 at 17:55
  • But if Eclipse does not recognize the method how would that be Ant's problem? – Dave Brock Oct 22 '11 at 17:57
  • You told that you manually compiled using ant. If you edit the `build.xml`, Eclipse will use it. The symptoms indicate that the manually edited `build.xml` is wrong. The remnant looks all fine, you've a Servlet 3.0 compatible container, you've a Dynamic Web Project 3.0, you've a `web.xml` complying Servlet 3.0 and you have nothing manually added in `/WEB-INF/lib` or buildpath properties (and I assume also not in `JRE/lib` or `JRE/lib/ext`). – BalusC Oct 22 '11 at 17:59
  • I'm using Eclipse's own compiler and it still does not recognize the method in the source file. – Dave Brock Oct 22 '11 at 18:14
  • Then you're not using a servlet 3.0 compatible container, or the dynamic web project version is not set to 3.0, or the `web.xml` does not comply Servlet 3.0, or you have servlet API libraries of an older servletcontainer version in `/WEB-INF/lib`, `JRE/lib`, `JRE/lib/ext` or elsewhere as referenced in project's build path. – BalusC Oct 22 '11 at 18:16
  • This answer is wrong for me. I'm using WebSphere 8.5 which is servlet 3.0 and im still getting this error. I didn't edit the build path but you don't say specifically which jars I need to delete from my lib folder. – Philip Rego Dec 26 '18 at 17:21
1

I've had the same trouble recently. In fact it started happening after adding some new jars. Ant found HttpServletRequest class in selenium-server.jar which alphabetically comes first before servlet-api.jar (which was supposed to be used). So i just renamed selenium-server.jar to x-selenium-server.jar and everything started building OK, as it used to.

Vitalii
  • 11
  • 1
0

This is not a problem with your java compiler. javax is provided by servlet container itself and you must include servlet container jar files to your project setup.

javax.servlet.http and all classes related servlet context and servlet programming is related to your Servlet Container only. So stop worrient about anything else and check if Tomcat libraries are being included in your WEB-APP class path.

If not add them and everything will be fine.

Right Click on your project > Properties > Add Libraries > Server Runtime

and choose your server that is associated with your application.

You are done, this will include Servlet Container libraries to your project and HttpServletRequest & HttpServletResponse classes will be resolved.

Hope it helps, more information about Servlet Architecture and context can be found Here.

neel4soft
  • 507
  • 1
  • 4
  • 12