Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to compile class for JSP

 
Tripti Ag
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Directory Structure:
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\MyJSP3\WEB-INF\classes\Person.class
Under WEB-INF, I have web.xml.
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\MyJSP3\PersonJsp


JSP Code:
<%@ page import="Person" %>
<html>
<head></head>
<body>
<jsp:useBean id="person" class="Person" scope="request" />
<jsp:setProperty name="person" property="name" value="Tripti" />
This JSP Page is created by:<jsp:getProperty name="person" property="name" />

</body>
</html>

--->Person.java (resides in default package; I know not a good practice at all)

import java.io.Serializable;

public class Person implements Serializable
{
private String name;
private String email;
private String phone;

public Person()
{
name="";
email="";
phone="";
}

public void setName(String n)
{
name=n;
}
public String getName()
{
return name;
}
public void setEmail(String e)
{
email=e;
}
public String getEmail()
{
return email;
}
public void setPhone(String p)
{
phone=p;
}
public String getPhone()
{
return phone;
}
}

---->web.xml
<web-app>
<servlet>
<servlet-name>ServletJSP3 </servlet-name>
<jsp-file>/PersonJsp.jsp</jsp-file>
</servlet>

<servlet-mapping>
<servlet-name>ServletJSP3 </servlet-name>
<url-pattern>/PersonJsp.jsp</url-pattern>
</servlet-mapping>

</web-app>


--->Error:
HTTP Status 500 - Unable to compile class for JSP:

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: [C:\Program Files\Apache Software Foundation\Tomcat 7.0\work\Catalina\localhost\MyJSP3\org\apache\jsp\PersonJsp_jsp.java]
The import Person cannot be resolved

An error occurred at line: 5 in the jsp file: /PersonJsp.jsp
Person cannot be resolved to a type
2: <html>
3: <head></head>
4: <body>
5: <jsp:useBean id="person" class="Person" scope="request" />
6: <jsp:setProperty name="person" property="name" value="Tripti" />
7: This JSP Page is created by:<jsp:getProperty name="person" property="name" />
8:


An error occurred at line: 5 in the jsp file: /PersonJsp.jsp
Person cannot be resolved to a type
2: <html>
3: <head></head>
4: <body>
5: <jsp:useBean id="person" class="Person" scope="request" />
6: <jsp:setProperty name="person" property="name" value="Tripti" />
7: This JSP Page is created by:<jsp:getProperty name="person" property="name" />
8:


An error occurred at line: 5 in the jsp file: /PersonJsp.jsp
Person cannot be resolved to a type
2: <html>
3: <head></head>
4: <body>
5: <jsp:useBean id="person" class="Person" scope="request" />
6: <jsp:setProperty name="person" property="name" value="Tripti" />
7: This JSP Page is created by:<jsp:getProperty name="person" property="name" />
8:


An error occurred at line: 7 in the jsp file: /PersonJsp.jsp
Person cannot be resolved to a type
4: <body>
5: <jsp:useBean id="person" class="Person" scope="request" />
6: <jsp:setProperty name="person" property="name" value="Tripti" />
7: This JSP Page is created by:<jsp:getProperty name="person" property="name" />
8:
9: </body>
10: </html>


An error occurred at line: 7 in the jsp file: /PersonJsp.jsp
Person cannot be resolved to a type
4: <body>
5: <jsp:useBean id="person" class="Person" scope="request" />
6: <jsp:setProperty name="person" property="name" value="Tripti" />
7: This JSP Page is created by:<jsp:getProperty name="person" property="name" />
8:
9: </body>
10: </html>


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
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.init(JspServlet.java:138)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

 
Tripti Ag
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I come to know bean must reside in a package, so under WEB-INF\classes, I have created subfolders com\test
and put .class file under it.

Changed jsp code is as follows:

<%@ page import="com.Test.Person" %>
<html>
<head></head>
<body>
<jsp:useBean id="person" class="com.Test.Person" scope="request" />
<jsp:setProperty name="person" property="name" value="Tripti" />
This JSP Page is created by:<jsp:getProperty name="person" property="name" />

</body>
</html>

Now getting error:

HTTP Status 500 - Servlet.init() for servlet ServletJSP3 threw exception

type Exception report

message Servlet.init() for servlet ServletJSP3 threw exception

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

exception

javax.servlet.ServletException: Servlet.init() for servlet ServletJSP3 threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

root cause

java.lang.NoClassDefFoundError: com/Test/Person (wrong name: Person)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2918)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1174)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1669)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1217)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1181)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2433)
org.apache.jasper.compiler.Node$Root.accept(Node.java:474)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
org.apache.jasper.compiler.Generator.generate(Generator.java:3517)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
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.init(JspServlet.java:138)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags for code, logs, xml etc. It's really difficult to read this thread in its current form.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you add a package statement to the Java source file? Move it to the package folder?
 
Tripti Ag
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault: I want to know why we need to keep source file in a package.
Also I was referring Head First Servlets, I want to know why development environment and deployment environment have been separated in that book.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because as of JDK 1.4 the default package is no longer automatically included. It's a Java thing, and is the same for servlets. Bottom line: always put all of your classes in a package.

Keeping development and deployment separate just makes good sense. You don't deploy the source, so there's no need to have everything all jumbled up.
 
Tripti Ag
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear Bibeault.
Problem has been resolved but some understanding is needed.
When you told me to put bean inside a package. Then I created subfolders under webapps as MyJSP3\com\Test\Person.java. Problem didn't solve by this.
But when I created development environment as follows:
c:\>MyApp
|____classes
| |____com
| |____Test(must be test)
| |_____Person.class
|____src
| |_____Person.java
|
|____etc
| |____web.xml
|____web
|____PersonJsp.java
Problem resolved. Don't know how??

 
K. Tsang
Bartender
Posts: 3502
14
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you first compiled the Person class it has no package statement in the source. So by simply moving/putting it in a folder will not work.

On the flip side after you add in the package statement and recompile then everything is in place already.
 
Tripti Ag
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks K. Tsang!! Got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic