[英]Date cannot be resolved to a type
我正在學習調試,並且通過瀏覽器運行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指令添加導入:
<%@page import="java.io.*, java.util.Date, java.util.Enumeration" %>
那就是。 您永遠不要在JSP中使用scriptlet。 將Java代碼放入控制器中,將JSP用作純視圖組件,其唯一目標是使用JSP EL,JSTL和其他cutom標簽生成標記。
import指令的語法錯誤。 正確的一個是:
<%@ page import="java.util.Date,java.io.*,java.util.Enumeration"%>
僅供參考:在jsp
使用scriptlets
不是一個好習慣。 將所有邏輯放在服務器端類中,僅將jsp用於顯示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.