“Import cannot be resolved” with JSP

我与影子孤独终老i 提交于 2019-12-10 03:10:31

问题


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>

回答1:


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

    <%@ page import="pkgname.Class1"%>



回答2:


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>



回答3:


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



来源:https://stackoverflow.com/questions/13755588/import-cannot-be-resolved-with-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!