request.getServletContext() not found, even with n

2019-02-06 01:35发布

问题:

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.

回答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:

  • How do I import the javax.servlet API in my Eclipse project?
  • Maven dependency for Servlet 3.0 API?


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



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



回答4:

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.