Java 日期无法解析为类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22448499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:46:29  来源:igfitidea点击:

Date cannot be resolved to a type

javadebuggingtomcat

提问by Opjeezzeey

I am learning debugging and I am getting the following error when running tomcat through my browser. I am using Texpad to write and I guess tomcat after.

我正在学习调试,通过浏览器运行 tomcat 时出现以下错误。我正在使用 Texpad 进行编写,我猜想是在使用 tomcat 之后。

An error occurred at line: 18 in the jsp file: /Debug.jsp
Date cannot be resolved to a type
15: 
16: <%
17:     response.setContentType("MIME");
18:     Date today = new Date(12,20,2004);
19: 
20:     Date created = new Date(session.getCreationTime());
21:     Date lastAccessed = new Date(session.getLastAccessedTime());

I am getting the same error twice for lines 18, 20, and 21. The original code is below.

对于第 18、20 和 21 行,我两次收到相同的错误。原始代码如下。

<HTML>
<HEAD>
<TITLE>JSP Debugging</TITLE>
</HEAD>

<BODY>
<% import java.io.*; %>
<% import java.util.Date; %>
<% import java.util.Enumeration; %>

<%
    response.setContentType("MIME");
    Date today = new Date(12,20,2004);

    Date created = new Date(session.getCreationTime());
    Date lastAccessed = new Date(session.getLastAccessedTime());

    out.print("<h1>Today is " );
    out.print(today); 
    out.print("</h1>" );
    out.print("This session has the following characteristics:<br>" );
    out.println("<br>ID: ");
    session.getId(); %>
    out.println("Created: " + created);
    out.println("Last Accessed: " + lastAccessed);
    out.println("<br>Max Inactive Interval: " +
                    session.getMaxInactiveInterval());
%>
</BODY>
</HTML>

I know there are many more errors and I am working on those but for now any help on this would be amazing. As far as I could tell it is an issue with Date but I am not sure what exactly.

我知道还有更多错误,我正在处理这些错误,但现在对此的任何帮助都将是惊人的。据我所知,这是 Date 的问题,但我不确定到底是什么。

EDIT-------------------------------------------------------------------------------------------------

编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ------------------------------------------------

So I made the changes requested and the code now looks like the following:

所以我进行了请求的更改,代码现在如下所示:

<HTML>
<HEAD>
<TITLE>JSP Debugging</TITLE>
</HEAD>

<BODY>
<%@ page import="java.util.Date,java.io.*,java.util.Enumeration"%>

<%
    response.setContentType("MIME");
    Date today = new Date(12,20,2004);

    Date created = new Date(session.getCreationTime());
    Date lastAccessed = new Date(session.getLastAccessedTime());

    out.print("<h1>Today is " );
    out.print(today); 
    out.print("</h1>" );
    out.print("This session has the following characteristics:<br>" );
    out.println("<br>ID: ");
    session.getId(); %>
    out.println("Created: " + created);
    out.println("Last Accessed: " + lastAccessed);
    out.println("<br>Max Inactive Interval: " +
                    session.getMaxInactiveInterval());
%>
</BODY>
</HTML>

And after I ran localhost:8080/Debug.jsp in my web browser it downloaded a new copy of the jsp with the expected results but they were supposed to be displayed in the browser.

在我的 Web 浏览器中运行 localhost:8080/Debug.jsp 后,它下载了一份具有预期结果的新 jsp 副本,但它们应该显示在浏览器中。

<HTML>
<HEAD>
<TITLE>JSP Debugging</TITLE>
</HEAD>

<BODY>


<h1>Today is Tue Feb 25 00:00:00 EST 1919</h1>This session has the following characteristics:<br><br>ID: 

    out.println("Created: " + created);
    out.println("Last Accessed: " + lastAccessed);
    out.println("<br>Max Inactive Interval: " +
                    session.getMaxInactiveInterval());
%>
</BODY>
</HTML>

回答by JB Nizet

You're not importing types correctly. An import must be added using the page directive:

您没有正确导入类型。必须使用 page 指令添加导入:

<%@page import="java.io.*, java.util.Date, java.util.Enumeration" %> 

That said. You should never use scriptlets in JSPs. Put Java code in controllers, use JSPs as pure view components whose unique goal is to generate markup using the JSP EL, the JSTL and other cutom tags.

那说。您永远不应该在 JSP 中使用 scriptlet。将 Java 代码放在控制器中,将 JSP 用作纯视图组件,其唯一目标是使用 JSP EL、JSTL 和其他 cutom 标记生成标记。

See How to avoid using scriptlets in my JSP page?

请参阅如何避免在我的 JSP 页面中使用 scriptlet?

回答by Sanjeev

Syntax of import directive is wrong. Correct one is:

导入指令的语法错误。正确的一种是:

<%@ page import="java.util.Date,java.io.*,java.util.Enumeration"%>

FYI: It is not good practice to use scriptletsin jsp. Put all your logic in server side class and use jsp only for display.

仅供参考:scriptletsjsp. 将所有逻辑放在服务器端类中,并仅使用 jsp 进行显示。