Java / JSP WEB-INF / classes cannot import

It has been some time since I had to make Java / JSP ...

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

On my jsp page I have

<%@ page import="MyClass" %> 

Tomcat says import cannot be allowed ...

I tried putting MyClass in a package (WEB-INF / com / MyClass) and then import the package into a Jsp file. Import no longer causes an error, but I cannot create a MyClass object, says the type is not allowed ...

What am I doing wrong here?

All help is appreciated! :)

+3
source share
4 answers

OMG, I found my mistake ...

Netbeans did not copy the lib files to the right folder, my jsp page was updating, so it looked like all the files were copied, but in fact MyClass.class was not in the folder ...

Thank you for your help!

0
source
 WEB-INF/classes/MyClass.java 

leads me to assume that you are using the default package, which is not good practice at all. Try to assign your class to the package and import according to this.

Do something like:

 package myPackage; class myClass { ... } 

And then:

 <%@ page import="myPackage.myClass" %> 
+4
source

The .class file should be placed in the classes folder in the WEB-INF section. Thus, the location of MyClass.class should be WEB-INF / classes / com / (in the case of com, it is a package).

 <% // Instantiate a MyClass com.MyClass obj=new com.MyClass(); %> 

OR

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

Which package does MyClass include? If the package is the default, you can put the class file in

 WEB-INF/classes 

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

0
source

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


All Articles