eclipse jsp方法getServletContext()在eclipse中未定义类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19934207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:20:15  来源:igfitidea点击:

jsp The method getServletContext() is undefined for the type in eclipse

eclipsejspscriptlet

提问by subash

I am using the eclipse helper for Java EE development. I am developing a Struts web application. In a JSP page I use code like this

我正在使用 eclipse helper 进行 Java EE 开发。我正在开发一个 Struts Web 应用程序。在 JSP 页面中,我使用这样的代码

<script type="text/javascript" src="<%out.print(getServletContext().getContextPath());%>/js/jquery.js"></script>
<link rel="stylesheet" href="<%out.print(getServletContext().getContextPath());%>/css/home.css" />

When I run my server this page works correctly. However, Eclipse shows an error on getServletContext()

当我运行我的服务器时,这个页面工作正常。但是,Eclipse 显示错误getServletContext()

The method getServletContext() is undefined for the type __2F_Compiler_2F_WebContent_2F_pages_2F_home_2E_jsp

__2F_Compiler_2F_WebContent_2F_pages_2F_home_2E_jsp 类型的 getServletContext() 方法未定义

Here's a screenshot:

这是一个屏幕截图:

screenshot

截屏

Because the page works correctly, I'd like to hide this error in Eclipse. How can I do that?

由于页面工作正常,我想在 Eclipse 中隐藏此错误。我怎样才能做到这一点?

回答by BalusC

I can't speak for Eclipse's mistake, but in this specific case, you should be using the implicit JSP scriptlet variable applicationand not be using HttpServlet-inherited methods.

我不能说 Eclipse 的错误,但在这种特定情况下,您应该使用隐式 JSP scriptlet 变量application而不是使用HttpServlet继承的方法。

<script type="text/javascript" src="<%out.print(application.getContextPath());%>/js/jquery.js"></script>
<link rel="stylesheet" href="<%out.print(application.getContextPath());%>/css/home.css" />


Unrelatedto the concrete problem, this is a 90's way of writing JSPs which is officially discouragedsince JSP 2.0 which was released more than a decade ago. Are you sure you're reading up to date resources while learning JSP? You should be using EL expressions instead.

具体问题无关,这是 90 年代编写 JSP 的方式,自从十多年前发布的 JSP 2.0 以来,官方不鼓励这种方式。您确定在学习 JSP 的同时阅读了最新的资源吗?您应该改用 EL 表达式。

<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/home.css" />

Or, via <c:set>to save the boilerplate:

或者,通过<c:set>保存样板:

<c:set var="root" value="${pageContext.request.contextPath}" />
...
<script type="text/javascript" src="${root}/js/jquery.js"></script>
<link rel="stylesheet" href="${root}/css/home.css" />

See also:

也可以看看:

回答by user3339968

If you use Tomcat 6.0, you should add extended Servlet.

如果你使用Tomcat 6.0,你应该添加扩展的Servlet。

Project->Properties->Add Library->Server Runtime

项目->属性->添加库->服务器运行时

And it will add the apache's lib.

它将添加 apache 的库。