request.getServletContext() not found, even with new JAR

43,584

Solution 1

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:

Solution 2

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....

Solution 3

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.

Share:
43,584
Dave Brock
Author by

Dave Brock

Updated on January 20, 2020

Comments

  • Dave Brock
    Dave Brock over 4 years

    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.

  • Dave Brock
    Dave Brock over 12 years
    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?
  • BalusC
    BalusC over 12 years
    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).
  • Dave Brock
    Dave Brock over 12 years
    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: <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xs‌​d" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">.
  • BalusC
    BalusC over 12 years
    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.
  • Dave Brock
    Dave Brock over 12 years
    I checked your other page and ensured everything is up to the correct version and I don't have any other dependencies. So frustrating!
  • BalusC
    BalusC over 12 years
    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.
  • Dave Brock
    Dave Brock over 12 years
    I'm compiling using Ant and this time, Eclipse does not understand the method in the source code view.
  • BalusC
    BalusC over 12 years
    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?
  • Dave Brock
    Dave Brock over 12 years
    But if Eclipse does not recognize the method how would that be Ant's problem?
  • BalusC
    BalusC over 12 years
    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).
  • Dave Brock
    Dave Brock over 12 years
    I'm using Eclipse's own compiler and it still does not recognize the method in the source file.
  • BalusC
    BalusC over 12 years
    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.
  • Asaph
    Asaph over 10 years
    What if you want to get the ServletContext in Servlet spec < 3.0 without starting a session?
  • Philip Rego
    Philip Rego over 5 years
    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.