如何从 JSP 页面引用 java .class 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1514960/
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-10-29 16:51:22  来源:igfitidea点击:

how to reference a java .class file from a JSP page?

javajsp

提问by Sajee

I have the JSP file (/page.jsp) in the root of my app directory. I want to use this class located in /WEB-INF/classes/Helper.class.

我的应用程序目录的根目录中有 JSP 文件 (/page.jsp)。我想使用位于/WEB-INF/classes/Helper.class 中的这个类。

I tried using the JSP page import statment with the class name but that didn't work. How can I reference Helper.class so that I can use it my JSP? I don't want to include the class in a package/JAR.

我尝试使用带有类名的 JSP 页面导入语句,但没有奏效。我如何引用 Helper.class 以便我可以在我的 JSP 中使用它?我不想在包/JAR 中包含该类。

回答by Billy Bob Bain

Okay, I didn't know this until I looked it up. The JSP Spec (JSP.11.2 JSP Page Implementation Class)is your friend. You'll need to move that class from the default package.

好吧,我不知道这个,直到我查了一下。该JSP规格(JSP.11.2 JSP页实现类)是你的朋友。您需要从默认包中移动该类。

As of JSP 2.0, it is illegal to refer to any classes from the unnamed (a.k.a. default) package. This may result in a translation error on some containers, specifically those that run in a JDK 1.4 or greater environment. It is unfortunate, but unavoidable, that this will break compatibility with some older JSP applications. However, as of JDK 1.4, importing classes from the unnamed package is not valid (see http://java.sun.com/j2se/1.4/compatibility.html#sourcefor details). Therefore, for forwards compatibility, applications must not rely on the unnamed package. This restriction also applies for all other cases where classes are referenced, such as when specifying the class name for a tag in a TLD

从 JSP 2.0 开始,引用未命名(也称为默认)包中的任何类都是非法的。这可能会导致某些容器的转换错误,特别是那些在 JDK 1.4 或更高版本环境中运行的容器。不幸的是,这将破坏与一些较旧的 JSP 应用程序的兼容性,但这是不可避免的。但是,从 JDK 1.4 开始,从未命名的包中导入类是无效的( 有关详细信息,请参见 http://java.sun.com/j2se/1.4/compatibility.html#source)。因此,为了向前兼容,应用程序不能依赖未命名的包。此限制也适用于引用类的所有其他情况,例如在 TLD 中为标签指定类名时

回答by ChssPly76

If your class is located directlyin /WEB-INF/classesthat means it uses default package which is generally not recommended. You don't need to import it because of that; you can use it directly in your JSP:

如果您的类直接位于/WEB-INF/classes这意味着它使用通常不推荐的默认包。因此,您不需要导入它;你可以直接在你的 JSP 中使用它:

<%
 Helper helper = new Helper(); // use appropriate constructor
 %>

A better solution would be to make it a part of package. You'd need to put it into appropriate subfolder of /WEB-INF/classesthen, say /WEB-INF/classes/com/mypackage/Helper.class. You'll use fully qualified name or import it in your JSP:

更好的解决方案是将其作为包的一部分。您需要将其放入/WEB-INF/classesthen 的适当子文件夹中,例如/WEB-INF/classes/com/mypackage/Helper.class. 您将使用完全限定名称或将其导入您的 JSP:

<%
 com.mypackage.Helper helper = new com.mypackage.Helper(); // use appropriate constructor
 %>

回答by Koekiebox

The following has to work <%@ page import="com.*" %>.

以下必须工作<%@ page import="com.*" %>

Check the documentation of the J2EE container you are using. If you are using a J2EE Sun Certified Container you shouldn't have an issue with the page import directive.

检查您正在使用的 J2EE 容器的文档。如果您使用的是 J2EE Sun 认证容器,那么页面导入指令应该不会有问题。

See JSP Directives.

请参阅JSP 指令

回答by duffymo

It has to be in the CLASSPATH of your WAR - either a package under WEB-INF or JAR under WEB-INF/lib. That's just basic Java.

它必须在您的 WAR 的 CLASSPATH 中 - WEB-INF 下的包或 WEB-INF/lib 下的 JAR。那只是基本的Java。

The object has to be in request, page, session, or application scope. This usually means that a servlet put it there. You've got to have a servlet and JSP collaborating to do it.

对象必须在请求、页面、会话或应用程序范围内。这通常意味着 servlet 将它放在那里。您必须有一个 servlet 和 JSP 协作才能做到这一点。

You can write scriptlet code, but I think it's far better to use JSTL. Scriptlet-free JSPs are a good idea in the long run.

您可以编写scriptlet 代码,但我认为使用JSTL 会好得多。从长远来看,无 Scriptlet 的 JSP 是一个好主意。

回答by Chii

try something like this: <jsp:useBean id="now" class="java.util.Date"/>

尝试这样的事情: <jsp:useBean id="now" class="java.util.Date"/>

the above creates an instance of Date and adds it as the request attribute map key now. It is then available for use, just like any other request attribute variable, e.g., inside el expressions such as ${now.time}will print the time in milliseconds.

以上创建了一个 Date 实例并将其添加为请求属性映射键now。然后它就可以使用了,就像任何其他请求属性变量一样,例如,在 el 表达式中,例如${now.time}将以毫秒为单位打印时间。

So in your scenario, you'd do <jsp:useBean id="Helper" class="com.your.company.name.Helper"/>. Make sure Helper has a no arg public constructor.

所以在你的场景中,你会做<jsp:useBean id="Helper" class="com.your.company.name.Helper"/>. 确保 Helper 没有 arg 公共构造函数。

extra info here http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html

这里的额外信息http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html