HTTP Status 500 - Unable to compile class for JSP

Roberto Rizzi picture Roberto Rizzi · Jul 26, 2013 · Viewed 16.1k times · Source
HTTP Status 500 - Unable to compile class for JSP

Ok, there are lots of articles about this error and i'm still a newbie. I sow that this error might be caused by some different kind of reasons and this is my (likely either stupid or already existent) reason:

It doesn't find the class, apparently imported from NetBeans.

<%@ page import="mypack.Display" %>

In fact, if i have a look to the cache file "index_jsp.java" the issue is just on the line where there's "import mypack.Display;". My problem is so similar to this What causes this jsp Error 500? with the difference that i didn't put mypack.Display into the default package, then i don't know why there's this error.

This is my index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1><% out.print("jsp class test");%></h1>
        <%@ page import="mypack.Display" %>   
        <%
            Display t = new Display();
            t.test("Roby");
        %>
    </body>
</html>

and this is the simple Display.java

package mypack;

public class Display{

  public void test(String msg){
    System.out.println("My name is "+msg);
  }
 }

A friend of mine, said me that i should configure the servlet into the web.xml file into the WEB-INF folder, but looking the samples i didn't understand how i can do it. Do i have to mapping the package into that file?

I put the Display.java under the package "mypack" correctly taken by Netbeans, in fact if i try to change either the class or the package name, both in the index.jsp and Display.java, the compiler shows me an error.

Could someone help me please for a better understanding?

Below the complete error:

type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 14 in the generated java file
Only a type can be imported. mypack.Display resolves to a package

An error occurred at line: 19 in the jsp file: /index.jsp
Display cannot be resolved to a type
16:         <%--<jsp:useBean id="link" scope="application" class = "mypack.Display" />--%>
17:         <%@ page import="mypack.Display" %>   
18:         <%
19:             Display t = new Display();
20:             t.test("Roby");
21: //          out.println(outpt);
22:         %>


An error occurred at line: 19 in the jsp file: /index.jsp
Display cannot be resolved to a type
16:         <%--<jsp:useBean id="link" scope="application" class = "mypack.Display" />--%>
17:         <%@ page import="mypack.Display" %>   
18:         <%
19:             Display t = new Display();
20:             t.test("Roby");
21: //          out.println(outpt);
22:         %>


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.35 logs.

Answer