日期无法解析为某种类型

时间:2014-03-17 07:00:30

标签: java debugging tomcat

我正在学习调试,当我通过浏览器运行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有关,但我不确定究竟是什么。

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

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

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

在我的网络浏览器中运行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>

2 个答案:

答案 0 :(得分:1)

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

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

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

请参阅How to avoid using scriptlets in my JSP page?

答案 1 :(得分:0)

import指令的语法错误。正确的是:

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

仅供参考:在scriptlets中使用jsp不是一个好习惯。将所有逻辑放在服务器端类中,仅使用jsp进行显示。