How to link to java.class file from JSP page?

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

I tried using the import status of the JSP page with the class name, but that did not work. How can I reference Helper.class so that I can use it in JSP? I do not want to include the class in the package / JAR.

+3
source share
5 answers

Well, I did not know this until I looked. JSP Spec (JSP.11.2 JSP) is your friend. You will need to transfer this class from the default package.

JSP 2.0, ( a.k.a. ). , , JDK 1.4 . , , JSP . , JDK 1.4, (. http://java.sun.com/j2se/1.4/compatibility.html#source ). , . , , , TLD

+6

/WEB-INF/classes, , , . - ; JSP:

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

. /WEB-INF/classes, /WEB-INF/classes/com/mypackage/Helper.class. JSP:

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

CLASSPATH WAR - WEB-INF JAR WEB-INF/lib. Java.

, , . , . JSP, .

, , JSTL. - JSP - .

0

<%@ page import="com.*" %>.

J2EE, . J2EE Sun Certified Container, .

. JSP Directives.

0

- : <jsp:useBean id="now" class="java.util.Date"/>

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

So, in your scenario you would do <jsp:useBean id="Helper" class="com.your.company.name.Helper"/>. Make sure Helper does not have a public arg constructor.

more information here http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html

0
source

Source: https://habr.com/ru/post/1719194/


All Articles