I am trying to call a Java class from a JSP page. I have created the project using JDeveloper.
I am getting an error that says "The import cannot be resolved". I have added the Class file in WEB-INF, root folder, and tried compiling, 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>
you should give fully qualified name for your class. (packagename.classname)
like:
<%@ page import="pkgname.Class1"%>
Page directives are normally placed at the top of a JSP. Also I assume Class1
is in the default package since it does not possess a fully qualified name. If Class1
is in a package you need to prefix the name 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>
First of all, /WEB-INF/src
is the wrong place to keep your java sources (since WEB-INF
folder contents are deployed to your server); you should want to move them out of /WEB-INF
(into /src in project root, for example)
Either way, you need to tell Eclipse where your sources are and where you want classes built to. It's done in project properties dialog:
Right-click on your project in Eclipse, select Properties
Click on Java Build path on the left
Click source tab on the right
Click Add Folder button and add your source folder (/WEB-INF/src or wherever you moved it to)
Ensure Allow output folders for source folders is checked below
Under newly added source path select output folder and point it to /WEB-INF/classes or other location of your choice