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

Hi I'm trying to compile a simple jsp file with tomcat but I kept having this error message

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

An error occurred at line: 13 in the jsp file: /WebContent/test.jsp
Member cannot be resolved to a type
10: <body>
11: <%  
12:     //MyBatisDao myDao = new MyBatisDao();
13:     List<Member> list; // = myDao.func("test");
14:     //out.println("Result list= " + list);
15: %>
16: </body>

test.jsp (at Webcontent)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="pageNumber.*, java.util.*, java.io.*" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%  
    List<Member> list;
%>
</body>
</html> 

Member.java

package pageNumber;
public class Member {
 private String id;
 private String pw;
 private int point;
 private int lvl;

 public Member() {}
 public Member(String id, String pw, int point, int lvl) {
  this.id = id;
  this.pw = pw;
  this.point = point;
  this.lvl = lvl;
 }

 public String getId() { return id; }
 public void setId(String id) { this.id = id; }
 public String getPw() { return pw; }
 public void setPw(String pw) { this.pw = pw; }
 public int getPoint() { return point; }
 public void setPoint(int point) { this.point = point; }     
 public int getLvl() { return lvl; }
 public void setLvl(int lvl) { this.lvl = lvl; }
 public String toString() {
  return "ID= " + id + ", PW= " + pw + ", POINT= " + point + ", LVL= " + lvl;
 }

}

As far as I can tell, I am having this problem because my tomcat cannot locate the class file, so I have placed my class files at

  • build/classes/pageNumber (where pageNumber is my package name)
  • WEB-INF/classes/pageNumber
  • tomcat\work\Catalina\localhost_\org\apache\jsp\pageNumber

but none of it worked out. Any help?


Edit: After I change my import declaration from

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

to

<%@ page import="java.util.List, pageNumber.Member" %>

It actually gave me a new type of error:

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

An error occurred at line: 15 in the generated java file
Only a type can be imported. pageNumber.Member resolves to a package

An error occurred at line: 12 in the jsp file: /WebContent/test.jsp
Member cannot be resolved to a type
9: </head>
10: <body>
11: <%  
12:     List<Member> list; // = myDao.func("test");
13: %>
14: </body>
15: </html> 

This question is related to java jsp tomcat

The answer is


There's no need to manually put class files on Tomcat. Just make sure your package declaration for Member is correctly defined as

package pageNumber;

since, that's the only application package you're importing in your JSP.

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

The problem is caused because you need to import the pageNumber.Member class in your JSP. Make sure to also include another packages and classes like java.util.List.

<%@ page import="pageNumber.*, java.util.*" %>

Still, you have a major problem by using scriptlets in your JSP. Refer to How to avoid Java Code in JSP-Files? and start practicing EL and JSTL and focusing more on a MVC solution instead.


Please remove the servlet jar from web project,as any how, the application/web server already had.


include your Member Class to your jsp :

<%@ page import="pageNumber.*, java.util.*, java.io.*,yourMemberPackage.Member" %>

This line of yours:

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

Requires an @ symbol before % like this:

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

I was facing the issue, i found that that older ecj library is present in Apache Tomcat directory 1)remove old jar from Apache Tomcat library 2)clean the project 3)build it. It started working as expected.


This maybe caused by jar conflict. Remove the servlet-api.jar in your servlet/WEB-INF/ directory, %Tomcat home%/lib already have this lib.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to jsp

Difference between request.getSession() and request.getSession(true) A child container failed during start java.util.concurrent.ExecutionException The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path Using if-else in JSP Passing parameters from jsp to Spring Controller method how to fix Cannot call sendRedirect() after the response has been committed? How to include js and CSS in JSP with spring MVC How to create an alert message in jsp page after submit process is complete getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? How to pass the values from one jsp page to another jsp without submit button?

Examples related to tomcat

Jersey stopped working with InjectionManagerFactory not found The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat Spring boot: Unable to start embedded Tomcat servlet container Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start Kill tomcat service running on any port, Windows Tomcat 8 is not able to handle get request with '|' in query parameters? 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE 403 Access Denied on Tomcat 8 Manager App without prompting for user/password Difference between Xms and Xmx and XX:MaxPermSize