I wrote a very simple jsp problem that gets a number from a class and show it but I encountered error 500.
I use eclipse with tomcat 5.5:
<%@page import="java.io.IOException"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<h1>number of shown :</h1>
<body>
<%
try{
out.print(Model.getCount());
}
catch(IOException k)
{
out.println("Eror");
}
%>
</body>
</html>
my Model class=>
public class Model {
static int number=0;
public static int getCount()
{
number++;
return number;
}
}
any my model class is default package.
The error I see:
type Exception report
message
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
jsp file: /show.jsp Model cannot be
resolved 11: 12: <% 13: try{
14: out.print(Model.getCount()); 15: }
16: catch(IOException k) 17: {Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:328)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:307)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:565)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:309)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)note The full stack trace of the root
cause is available in the Apache
Tomcat/5.5.33 logs.
Apache Tomcat
Best Answer
Never put any class in the default package. The default package is not accessible to classes in a package, and since the JSP is transformed in a servlet class by Tomcat, and this class is in a package, it can't access your Model class.
Once it's in a well-defined package, you'll have to import it, as mschonaker shows in his answer: