"Import cannot be resolved" with JSP
Asked Answered
S

4

9

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>
Sg answered 7/12, 2012 at 1:31 Comment(1)
This should solve your problem. #1541357Kileykilgore
H
5

you should give fully qualified name for your class. (packagename.classname) like:

    <%@ page import="pkgname.Class1"%>
Heres answered 7/12, 2012 at 1:36 Comment(3)
the class is not packaged. Is packaging necessary?Sg
@Sg yes, you have to package your class's. its a bad practice if dont package em. check this link for more details coderanch.com/how-to/java/PackageYourBeansHeres
I had a similar error which brought me here. I had <%@ page import="main.java.model.*" %> which resulted in CompanyDAO not being resolved when the JSP ran. I repeated the line and replaced the * with CompanyDAO like this: <%@ page import="main.java.model.CompanyDAO" %>. Much to my surprise that resolved the issue. And yes, take the time to put all your code in packages. I don't care much for "CodeRanch". I figured out packages in Eclipse by converting the mess I had made into what Eclipse wanted for a Web app.Powdery
T
3

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>
Tetrabasic answered 7/12, 2012 at 1:34 Comment(6)
i dont think it'd really matter declaring them in the body tagHeres
@GanGnaMStYleOverFlowErroR this was more a style or best practice suggestion.Tetrabasic
@km yes, its a good practice, but wouldn't solve the compiler error , would it ?Heres
@GanGnaMStYleOverFlowErroR agreed. I'm also interested in if the class is packaged. I used to work with Jdeveloper, I feel for anyone using that "tool".Tetrabasic
@km as we can see from his JSp, that could be the only possible case.Heres
Hi all, thanks for quick replies.... The class "Class1" is not packaged. If I type a different class name (class that does not exists), then JDeveloper shows error "class not found" with yellow underline. If I type correct class name, editor error is resolved, however, I get that error when I compile. I have tried adding class path of that directory, but error kept showing up. Thanks.Sg
K
1

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:

  1. Right-click on your project in Eclipse, select Properties

  2. Click on Java Build path on the left

  3. Click source tab on the right

  4. Click Add Folder button and add your source folder (/WEB-INF/src or wherever you moved it to)

  5. Ensure Allow output folders for source folders is checked below

  6. Under newly added source path select output folder and point it to /WEB-INF/classes or other location of your choice

Kileykilgore answered 26/1, 2017 at 15:27 Comment(0)
G
0

You shouldn't repeat the directive tags like <%@ page import ......%>. But you can repeat the values in the import attribute value itself like <%@ page import="java.util.*, java.io.*"%> and make sure to put the fully qualified name to import

Groovy answered 8/3, 2021 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.