gpt4 book ai didi

java - 日期无法解析为类型

转载 作者:行者123 更新时间:2023-11-30 11:21:01 25 4
gpt4 key购买 nike

我正在学习调试,在通过浏览器运行 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());

我在第 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>

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

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

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

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

在我的 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>

最佳答案

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

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

就是说。永远不要在 JSP 中使用 scriptlet。将 Java 代码放在 Controller 中,使用 JSP 作为纯 View 组件,其唯一目标是使用 JSP EL、JSTL 和其他自定义标记生成标记。

参见 How to avoid using scriptlets in my JSP page?

关于java - 日期无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22448499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com