我正在學習Servlets/jsps並編寫了一些測試類。一切似乎按預期工作,我遇到的唯一問題是能夠編譯一個簡單的Java類。這是類:類不會在Eclipse中的動態Web項目中編譯
package ilya.model;
public class DatabaseConnection {
public String getConnection()
{
String result;
try {
Class.forName("org.postgresql.Driver");
System.out.println("found the driver");
result = "Connection established!";
}
catch (ClassNotFoundException e)
{
System.out.println("No driver");
result = "No Connection";
}
return result;
}
}
試圖訪問它的JSP非常簡單,我不認爲這有什麼關係呢。如果有人想讓我發佈,請告訴我。
這是我收到的時候類被初始化首次異常:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 15 in the generated java file
Only a type can be imported. ilya.model.DatabaseConnection resolves to a package
這編譯罰款普通的Java項目。有任何想法嗎?
更新 這是JSP文件。它現在實際上正在工作。我在另一臺機器上嘗試過相同的項目,並且一切正常。 Eclipse必須有問題。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="ilya.model.BeerSuggestor, ilya.model.DatabaseConnection" %>
<%!
int count=0;
String connect;
public void jspInit() {
ServletConfig sconfig = getServletConfig();
String lname = sconfig.getInitParameter("lastName");
ServletContext context = sconfig.getServletContext();
context.setAttribute("lastName", lname);
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%-- DatabaseConnection intialized here --%>
<%
DatabaseConnection db = new DatabaseConnection();
connect = db.getConnection();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>The count is: <%= this.count++ %></p>
<p>The count is: <%= 500 %></p>
<p>The count is: <%= config.getInitParameter("lastName") %></p>
<%-- Value of connect printed here --%>
<p>The connection result is: <%=" " + connect %>
</body>
</html>
問題出在您的JSP中,請張貼它。服務器無法編譯JSP,它不是關於您的Java類('無法編譯JSP的類')。 – home 2012-03-21 13:48:57
同意; JSP與它有關,也許你想如何導入。並不是說我們應該將類導入到JSP中。 – 2012-03-21 13:53:10
我也嘗試添加一個主要方法,並自行編譯/運行類。它也不會編譯。 – webhound 2012-03-21 14:19:51