Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 499
Dave Brock Avatar asked Oct 22 '11 16:10

Dave Brock


3 Answers

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

like image 124
Neha Velhal Avatar answered Oct 13 '22 18:10

Neha Velhal


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:

  • How do I import the javax.servlet API in my Eclipse project?
  • Maven dependency for Servlet 3.0 API?
like image 32
BalusC Avatar answered Oct 13 '22 20:10

BalusC


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.

like image 41
Vitalii Avatar answered Oct 13 '22 19:10

Vitalii