2

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.

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.

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

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.

<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>
Opjeezzeey
  • 43
  • 1
  • 3
  • 7
  • Ok I moved session.getID(); %> to the line just above %> and now it executes the code and everything but it still displays it in a textpad document and not in the web browser itself. – Opjeezzeey Mar 18 '14 at 05:53
  • That's not what you should do. You should simply removes the trailing `%>`, which ends the scriptlet. And the page directive should be at the top of the JSP. It's quite simple: everything inside between `<%` and `%>` is executed Java code. Everything outside is text. So if you close the scriptlet with `%>`, the following is interpreted as text. – JB Nizet Mar 18 '14 at 06:04
  • Alright so I did that and it now executes the code but it still provides it in a downloaded jsp and not in the web browser when I run http://localhost:8080/Debug.jsp – Opjeezzeey Mar 18 '14 at 06:28
  • Thank you for all of your help by the way. It is really starting to make sense to me. – Opjeezzeey Mar 18 '14 at 06:28

2 Answers2

1

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

<%@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.

See How to avoid using scriptlets in my JSP page?

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • So I would delete the initial imports and replace it with what you provided right? Also what is the reason for the difference? – Opjeezzeey Mar 17 '14 at 21:39
  • The reason is that the code inside `<% %>` (scriptlet) is code that goes inside the JSP service method. – JB Nizet Mar 17 '14 at 21:42
  • Ahh alright. So I just replace the imports with the page directive. – Opjeezzeey Mar 17 '14 at 22:57
  • Alright so my instructions for this have me make changes to the Debug.jsp in textpad and then save them. Then it has me start Tomcat and have the jsp (Debug.jsp) placed in Tomcat's ROOT folder. Then I am to run http://localhost:8080/Debug.jsp in a web browser and that is what provided me the error I gave you here. Now when I run it my computer just downloads a copy of the jsp file and opens it in textpad. I added the change you advised. I will post the new code below. – Opjeezzeey Mar 18 '14 at 03:51
0

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 scriptlets in jsp. Put all your logic in server side class and use jsp only for display.

Sanjeev
  • 425
  • 10
  • 17