即使使用新的 JAR,也未找到 request.getServletContext()
我的编译器无法找到 HttpServletRequest getServletContext() 方法。
我没有做任何太复杂的事情:
public static void setMySortedSet(HttpServletRequest request, SortedSet<String> set)
{
setMySortedSet(request.getServletContext(), set);
}
我尝试过一些故障排除:
- 发现该方法是在 2.3 中创建的,因此我包含了一个反映这一点的 JAR(并将其放在我的 Eclipse 构建路径中)
- 我将该 JAR 包含在我的构建中.xml 类路径。
当我使用 Eclipse 时,找到了该方法,但是当我尝试构建类时,我看到了以下内容:
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
关于我可能缺少什么的任何想法吗?我很感激任何回应。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
getServletContext()
方法是在 Servlet 3.0 中引入的,而不是 2.3 中。但是如果你想获取 ServletContext ,那么另一种获取它的方法是:这样你就可以在不同的浏览器中获取存储的请求参数值......
The
getServletContext()
method is introduced in Servlet 3.0, not 2.3. But if you want to get theServletContext
then an alternative method to get it is:This way you can get the stored Request Parameter Value in different browser....
根据 Javadoc
ServletRequest#getServletContext()
方法是在 Servlet 3.0 中引入的,而不是 2.3 中。您需要安装并集成 Servlet 3.0 兼容容器,例如 Tomcat 7、Glassfish 3 等,并将动态 Web 项目的目标运行时设置为该容器。如果您正确执行此操作,那么您根本不需要手动修改构建路径或 build.xml,Eclipse 会自动为您处理。您也不需要需要下载不同品牌/版本的任意 servlet 容器的松散 JAR 文件并将其放入构建路径中。它只会导致未来的类路径和可移植性问题。另请参阅:
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 orbuild.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:
我最近也遇到了同样的麻烦。事实上,它是在添加一些新罐子后开始发生的。 Ant 在 selenium-server.jar 中找到了 HttpServletRequest 类,该类按字母顺序排在 servlet-api.jar (应该使用的)之前。
所以我只是将 selenium-server.jar 重命名为 x-selenium-server.jar,一切都开始正常构建,就像以前一样。
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.
这不是你的java编译器的问题。 javax 由 servlet 容器本身提供,您必须将 servlet 容器 jar 文件包含到项目设置中。
javax.servlet.http 和所有与 servlet 上下文相关的类以及 servlet 编程仅与您的 Servlet 容器相关。因此,不要担心其他任何事情,检查 Tomcat 库是否包含在您的 WEB-APP 类路径中。
如果不添加它们,一切都会好起来的。
右键单击您的项目>属性>添加库>服务器运行时
并选择与您的应用程序关联的服务器。
您已完成,这将包括 Servlet 容器库到您的项目以及 HttpServletRequest 和 HttpServletRequest 。 HttpServletResponse 类将被解析。
希望它有所帮助,有关 Servlet 架构和上下文的更多信息可以找到 这里。
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.