从jsp调用Java类
问题描述:
<%@ 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类
gives me following error how to load java class in jsp
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:
答
您需要为应用程序创建一个类似于
You need to create a package for the application which would be something like
com.yourappname.data
然后在该程序包中创建DBConenct类或在其中重构它,以便当您需要导入该类时,可以使用
then create your DBConenct class in that package or refactor it in there so that when you need to import the class you do so by using
import com.yourappname.data.DBConnect
您可以在jsp中导入和使用该类
The you can import and use the class in your jsp
<%@page import="com.yourappname.data.DBConnect"%>
另一方面,您不应在jsp中进行任何数据库访问,而应在servlet中进行所有数据访问.
On a side note, you shouldn't be doing any database acces within the jsp you should instead be doing all of your data access within a servlet.