"Impossible to import" with JSP

I am trying to call a Java class from a JSP page. I created a project using JDeveloper.

I get an error "Import could not be resolved." I added the class file to WEB-INF, the root folder and tried to compile, but it still shows the same error.

Below is the code:

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/> </head> <body> <p> <%@ page import="java.util.*"%> <%@ page import="Class1"%> <% Class1 tc=new Class1("test"); out.print(tc.str); %> </p> </body> </html> 
+6
source share
3 answers

You must give the full name for your class. (Packagename.classname) as:

  <%@ page import="pkgname.Class1"%> 
+2
source

Page directives are usually located at the top of the JSP. I also assume that Class1 is in the default package, as it does not have a fully qualified name. If Class1 is in the package, you need a name prefix in the import with the package name.

 <%@ page import="java.util.*" %> <%@ page import="Class1" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/> </head> <body> <p> <% Class1 tc=new Class1("test"); out.print(tc.str); %> </p> </body> 
+1
source

First of all, /WEB-INF/src is the wrong place to store your java sources (since the contents of the WEB-INF folder are deployed to your server); you should want to get them out of /WEB-INF (for example, in / src in the root of the project)

In any case, you need to tell Eclipse where your sources are and where you need classes. This is done in the project properties dialog box:

  • Right-click the project in Eclipse, select "Properties"

  • Click on the Java build path on the left

  • Click the source tab on the right.

  • Click the "Add Folder" button and add the source folder (/ WEB-INF / src or wherever you move it)

  • Make sure the output folders for source folders are listed below.

  • With the newly added source path, select the output folder and specify it in / WEB-INF / classes or another location of your choice.

0
source

All Articles