TAGS :Viewed: 10 - Published at: a few seconds ago

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

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.

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.

Answer 1

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.

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#source for 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

Answer 2

If your class is located directly in /WEB-INF/classes that 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:

 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/classes then, say /WEB-INF/classes/com/mypackage/Helper.class. You'll use fully qualified name or import it in your JSP:

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

Answer 3

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.

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.

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.

Answer 4

The following has to work <%@ 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.

See JSP Directives.

Answer 5

try something like this: <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.

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.

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