我正在学习调试,通过浏览器运行 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>
我知道还有更多错误,我正在处理这些错误,但现在对此的任何帮助都将是令人惊奇的。据我所知,这是日期的问题,但我不确定到底是什么。
编辑---------------------------------------------- -------------------------------------------------- -
所以我进行了请求的更改,代码现在如下所示:
<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 指令添加导入:
<%@page import="java.io.*, java.util.Date, java.util.Enumeration" %>
也就是说。切勿在 JSP 中使用 scriptlet。将 Java 代码放入 Controller 中,使用 JSP 作为纯 View 组件,其独特目标是使用 JSP EL、JSTL 和其他自定义标记生成标记。
关于java - 日期无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22448499/