简体   繁体   English

在Eclipse的Dynamic Web Project中无法编译类

[英]Class won't compile in Dynamic Web Project in Eclipse

I'm learning about Servlets/jsps and have some test classes written. 我正在学习Servlets / jsps,并编写了一些测试类。 Everything seems to work as expected, the only problem I'm running into is being able to compile a simple Java class. 一切似乎都按预期工作,我遇到的唯一问题是能够编译一个简单的Java类。 This is the class: 这是课程:

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;
}

} }

The jsp trying to access it is pretty simple and I don't think it has anything to do with it. 尝试访问它的jsp非常简单,我认为它与它无关。 If anyone wants me to post it let me know. 如果有人要我发帖,请告诉我。

Here's the Exception I receive when class is initialised for the first time: 这是我第一次初始化类时收到的异常:

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

This compile fine in a regular Java project. 这可以在常规Java项目中很好地编译。 Any ideas? 有任何想法吗?

Update Here is the JSP file. 更新这是JSP文件。 It's actually working now. 它实际上正在工作。 I tried the same project on a different machine and everything worked. 我在另一台机器上尝试了相同的项目,并且一切正常。 Must be something wrong with Eclipse. 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>

You cannot have plain Java in JSP files as you show here.. 如此处所示,您不能在JSP文件中包含纯Java。

Create a separate class in the appropriate source folder inside Eclipse for this. 为此,请在Eclipse内部的相应源文件夹中创建一个单独的类。

Agreed with previous comments. 同意先前的评论。 It looks like, you are trying to establish a database connection directly from your JSP. 看起来,您正在尝试直接从JSP建立数据库连接。

Please check the line in JSP, specially where you are setting the driver in jsp. 请检查JSP中的行,特别是在jsp中设置驱动程序的位置。

You can try this from your jsp as well. 您也可以从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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM