简体   繁体   中英

Java/JSP WEB-INF/classes can't import

Its been a while since I had to do some Java/JSP...

I have a java class in WEB-INF/classes/MyClass.java The build in Netbeans is successful and I can see MyClass.class in the classes folder.

In my jsp page, I've got

<%@ page import="MyClass" %>

Tomcat says that the import cannot be resolved...

I tried to put MyClass in a package(WEB-INF/com/MyClass) and then import the package into my Jsp file. The import doesnt throw an error anymore then, but I cannot instanciate a MyClass object, says the type is unresolved...

What am I doing wrong here..?

All help appreciated! :)

WEB-INF/classes/MyClass.java 

makes me guess that you're using the default package which is'nt a good practice at all. Try assigning your class to a package and do the import according to that.

Do something like:

package myPackage;

class myClass
{
...
}

And afterwards:

<%@ page import="myPackage.myClass" %>

.class file must be placed inside the classes folder under WEB-INF . So, the location of MyClass.class must be WEB-INF/classes/com/ (In case com is a package).

<%
 // Instantiate a MyClass

 com.MyClass obj=new com.MyClass();

%>

OR

<%@ page import="com.MyClass" %>
<%
   MyClass obj=new MyClass();
%>

What package is MyClass in? If the default package then you can put the class file in

WEB-INF/classes

if it's in a package, then use the package directory hierarchy under classes

OMG, I found my mistake...

Netbeans wasn't copying the lib files to the right folder, my jsp page was being updated, so it looked like all the files were copied, but actually MyClass.class wasnt in the folder...

Thanks for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM