tags:

views:

258

answers:

5

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.

A: 

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.

duffymo
+1  A: 

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
 %>
ChssPly76
Scriptlet code - I have to avert my eyes...it burns.
duffymo
I'm trying to call a static method of Helper but I get the error "Helper cannot be resolved"
Sajee
This is the code that causes the error above: Connection conn = Helper.getDBConnection(url,datasource);
Sajee
@duffymo - I'm not in any way **recommending** someone to use scriptlets - I myself have long abandoned JSP altogether in favor of templating. But - guessing from `Helper.getDBConnection()` and default package - I'm thinking it'll be more helpful to OP if I were to answer his question rather than try and recommend he use Spring MVC or something :-)
ChssPly76
A: 

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.

Koekiebox
+2  A: 

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

Wayne Young
A: 

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

Chii