0

我在两个 IDE 中都尝试了以下 jsp 文件。它在 NetBeans 中运行良好,但在 Eclipse 中无法运行。该程序是从 NTP 服务器获取时间。

代码如下

 <%-- 
Document   : GetNTP
Created on : May 21, 2013, 2:21:29 PM
Author     : Maximin
--%>
<%@page import="java.net.InetAddress"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import ="java.util.Date"%>

<%@page import="org.apache.commons.net.ntp.NTPUDPClient"%>
<%@page import="org.apache.commons.net.ntp.TimeInfo"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1><%
    String TIME_SERVER = "time.nist.gov";
    NTPUDPClient timeClient = new NTPUDPClient();
    InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
    TimeInfo timeInfo = timeClient.getTime(inetAddress);
    long returnTime = timeInfo.getReturnTime();
    Date time = new Date(returnTime);
            SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy");
            out.println(sdf.format(time));
    %></h1>
</body>
</html>

当我在 Eclipse 中运行它时,我得到了

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

An error occurred at line: 17 in the generated java file
Only a type can be imported. org.apache.commons.net.ntp.NTPUDPClient resolves to a package

An error occurred at line: 18 in the generated java file
Only a type can be imported. org.apache.commons.net.ntp.TimeInfo resolves to a package

 An error occurred at line: 18 in the jsp file: /GetTime.jsp
NTPUDPClient cannot be resolved to a type
15: <body>
16: <h1><%
17:         String TIME_SERVER = "time.nist.gov";
18:         NTPUDPClient timeClient = new NTPUDPClient();
19:         InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
20:         TimeInfo timeInfo = timeClient.getTime(inetAddress);
21:         long returnTime = timeInfo.getReturnTime();

为什么会这样?

我应该怎么做才能让它在 Eclipse 上运行?

4

1 回答 1

0

为我工作:

“右键单击您的项目并选择属性(它在底部......可能)。部署程序集是页面之一。如果带有 NTP 类的 jar 不在 WebContent/WEB-INF/lib 中或服务器上的其他位置,确保它在这个页面上。”

尼丁回答了

于 2013-05-23T04:12:34.373 回答