<%@ page language="java" import="DBConnect"%>


<%
try
{
System.out.println("\n--------------------");
System.out.println("\n loading ..");

DBConnect.connectToDb();

%>

<h3>Connection ok</h3>
<%
}
catch(Exception e)
{
e.printStackTrace();
}
%>


给我以下错误
如何在jsp中加载java类

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 12 in the generated java file
The import DBConnect cannot be resolved

An error occurred at line: 16 in the jsp file: /applicationservices/fileshare/vm/TestJdbc.jsp
DBConnect cannot be resolved
13: System.out.println("\n--------------------");
14: System.out.println("\n loading ..");
15:
16: DBConnect.connectToDb();
17:
18: %>
19:

最佳答案

您需要为应用程序创建一个程序包,类似于

com.yourappname.data


然后在该程序包中创建DBConenct类或在其中重构它,以便在需要导入该类时使用

import com.yourappname.data.DBConnect


您可以在jsp中导入和使用该类

<%@page import="com.yourappname.data.DBConnect"%>


附带说明一下,您不应在jsp中进行任何数据库访问,而应在servlet中进行所有数据访问。

09-05 21:38