[英]Class won't compile in Dynamic Web Project in Eclipse
我正在学习Servlets / jsps,并编写了一些测试类。 一切似乎都按预期工作,我遇到的唯一问题是能够编译一个简单的Java类。 这是课程:
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文件中包含纯Java。
为此,请在Eclipse内部的相应源文件夹中创建一个单独的类。
同意先前的评论。 看起来,您正在尝试直接从JSP建立数据库连接。
请检查JSP中的行,特别是在jsp中设置驱动程序的位置。
您也可以从jsp中尝试此操作。
<%
Class.forName("org.postgresql.Driver");
Connection myConn=DriverManager.getConnection("jdbcostgresql://localhost/db_name?user=db_user&password=db_pwd");
%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.