Call Java method from JSP file - java

I have a problem with calling Java method from .jsp file. I converted my project into Maven but still have the same problem.
My .jsp file is in /db-reagencii/WebContent/reagents/reagents.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#page import="cz.dbReagents.web.reagentsWeb.ReagentsWeb"%>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Reagencie</title>
</head>
<body>
<jsp:include page="../WEB-INF/menu/menu.jsp"/>
Reagents
<jsp:useBean id="podnikyWeb" class="cz.dbReagents.web.reagentsWeb.ReagentsWeb" scope="request"/>
</body>
</html>
and i want call method from this class
package cz.dbReagents.web.reagentsWeb;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import cz.dbReagents.dto.reagents.Reagents;
public class ReagentsWeb implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public Collection<Reagents> getReagents(){
ArrayList<Reagents> result = new ArrayList<Reagents>();
result.add(new Reagents("testovaci reagencie",11));
System.out.print(result.get(0).getName());
return result;
}
}
and this class
package cz.dbReagents.dto.reagents;
import java.io.Serializable;
public class Reagents implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int sum;
public Reagents(){
}
public Reagents(String name, int sum){
this.name = name;
this.sum = sum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
}
but when I tried to load page a got this error
VI 11, 2015 8:47:28 ODP. org.apache.catalina.core.StandardWrapperValve
invoke SEVERE: Servlet.service() for servlet [jsp] in context with
path [/db-reagencii] threw exception [/reagents/reagents.jsp (line:
17, column: 1) The value for the useBean class attribute
cz.dbReagents.web.reagentsWeb.ReagentsWeb is invalid.] with root cause
org.apache.jasper.JasperException: /reagents/reagents.jsp (line: 17,
column: 1) The value for the useBean class attribute
cz.dbReagents.web.reagentsWeb.ReagentsWeb is invalid. at
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
at
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
at
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
at
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1242)
at org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1196) at
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2392) at
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2444) at
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2450) at
org.apache.jasper.compiler.Node$Root.accept(Node.java:474) at
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2392) at
org.apache.jasper.compiler.Generator.generate(Generator.java:3529) at
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:251)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:374) at
org.apache.jasper.compiler.Compiler.compile(Compiler.java:354) at
org.apache.jasper.compiler.Compiler.compile(Compiler.java:341) at
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Is there a chance that I have bad configured server?
I have web.xml in /db-reagencii/WebContent/WEB-INF/web.xml and contains this:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Databáze Reagencií</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
I am using Tomcat 7.0.
This is my first web application in Java so I am sure that there is some stupid mistake but I can't find it.
Thanks for any advice :)
My error with scriplet.
VI 11, 2015 11:09:00 ODP.
org.apache.catalina.core.StandardWrapperValve invoke SEVERE:
Servlet.service() for servlet [jsp] in context with path
[/db-reagencii] threw exception [Unable to compile class for JSP:
An error occurred at line: [14] in the generated java file:
[/home/michal/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/db-reagencii/org/apache/jsp/reagents/reagents_jsp.java]
Only a type can be imported.
cz.dbReagents.web.reagentsWeb.ReagentsWebHandler resolves to a package
An error occurred at line: [15] in the generated java file:
[/home/michal/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/db-reagencii/org/apache/jsp/reagents/reagents_jsp.java]
Only a type can be imported. cz.dbReagents.dto.reagents.Reagents
resolves to a package
An error occurred at line: 18 in the jsp file: /reagents/reagents.jsp
ReagentsWebHandler cannot be resolved to a type 15: 16: Reagents 17: 18: <%
ReagentsWebHandler r = new ReagentsWebHandler(); %> 19: 20:
21:
An error occurred at line: 18 in the jsp file: /reagents/reagents.jsp
ReagentsWebHandler cannot be resolved to a type 15: 16: Reagents 17: 18: <%
ReagentsWebHandler r = new ReagentsWebHandler(); %> 19: 20:
21:
Stacktrace:] with root cause org.apache.jasper.JasperException: Unable
to compile class for JSP:
An error occurred at line: [14] in the generated java file:
[/home/michal/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/db-reagencii/org/apache/jsp/reagents/reagents_jsp.java]
Only a type can be imported.
cz.dbReagents.web.reagentsWeb.ReagentsWebHandler resolves to a package
An error occurred at line: [15] in the generated java file:
[/home/michal/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/db-reagencii/org/apache/jsp/reagents/reagents_jsp.java]
Only a type can be imported. cz.dbReagents.dto.reagents.Reagents
resolves to a package
An error occurred at line: 18 in the jsp file: /reagents/reagents.jsp
ReagentsWebHandler cannot be resolved to a type 15: 16: Reagents 17: 18: <%
ReagentsWebHandler r = new ReagentsWebHandler(); %> 19: 20:
21:
An error occurred at line: 18 in the jsp file: /reagents/reagents.jsp
ReagentsWebHandler cannot be resolved to a type 15: 16: Reagents 17: 18: <%
ReagentsWebHandler r = new ReagentsWebHandler(); %> 19: 20:
21:
Stacktrace: at
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:379) at
org.apache.jasper.compiler.Compiler.compile(Compiler.java:354) at
org.apache.jasper.compiler.Compiler.compile(Compiler.java:341) at
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
I got this error when I put only this lines into ragents.jsp
<%# page import="cz.dbReagents.web.reagentsWeb.ReagentsWebHandler " %> <h1>Hello World</h1>
VI 11, 2015 11:14:05 ODP.
org.apache.catalina.core.StandardWrapperValve invoke SEVERE:
Servlet.service() for servlet [jsp] in context with path
[/db-reagencii] threw exception [Unable to compile class for JSP:
An error occurred at line: [14] in the generated java file:
[/home/michal/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/db-reagencii/org/apache/jsp/reagents/reagents_jsp.java]
Only a type can be imported.
cz.dbReagents.web.reagentsWeb.ReagentsWebHandler resolves to a package
Stacktrace:] with root cause org.apache.jasper.JasperException: Unable
to compile class for JSP:
An error occurred at line: [14] in the generated java file:
[/home/michal/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/db-reagencii/org/apache/jsp/reagents/reagents_jsp.java]
Only a type can be imported.
cz.dbReagents.web.reagentsWeb.ReagentsWebHandler resolves to a package
Stacktrace: at
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:379) at
org.apache.jasper.compiler.Compiler.compile(Compiler.java:354) at
org.apache.jasper.compiler.Compiler.compile(Compiler.java:341) at
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
When I deploy war files into tomcat i got this error
VI 11, 2015 11:50:18 ODP. org.apache.catalina.core.ApplicationContext
log INFO: ContextListener: contextInitialized() VI 11, 2015 11:50:18
ODP. org.apache.catalina.core.ApplicationContext log INFO:
SessionListener: contextInitialized() VI 11, 2015 11:50:18 ODP.
org.apache.catalina.core.ApplicationContext log INFO: ContextListener:
attributeAdded('org.apache.jasper.compiler.TldLocationsCache',
'org.apache.jasper.compiler.TldLocationsCache#68ccc170') VI 11, 2015
11:50:45 ODP. org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path
[/db-reagencii] threw exception [Unable to compile class for JSP:
An error occurred at line: [14] in the generated java file:
[/home/michal/apps/apache-tomcat-7.0.62/work/Catalina/localhost/db-reagencii/org/apache/jsp/reagents/reagents_jsp.java]
Only a type can be imported.
cz.dbReagents.web.reagentsWeb.ReagentsWebHandler resolves to a package
Stacktrace:] with root cause org.apache.jasper.JasperException: Unable
to compile class for JSP:
An error occurred at line: [14] in the generated java file:
[/home/michal/apps/apache-tomcat-7.0.62/work/Catalina/localhost/db-reagencii/org/apache/jsp/reagents/reagents_jsp.java]
Only a type can be imported.
cz.dbReagents.web.reagentsWeb.ReagentsWebHandler resolves to a package
Stacktrace: at
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:379) at
org.apache.jasper.compiler.Compiler.compile(Compiler.java:354) at
org.apache.jasper.compiler.Compiler.compile(Compiler.java:341) at
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

you have an extra space before "page" in the JSP Import directive:
<%#page import="cz.dbReagents.web.reagentsWeb.ReagentsWeb"%>
It should be:
<%# page import="cz.dbReagents.web.reagentsWeb.ReagentsWeb"%>

I really hate using java sentences in JSP but if you want to do it this way, just modify your ReagentsWeb this way:
public class ReagentsWeb {
public Collection<Reagents> reagents;
public ReagentsWeb(){
reagents = this.getReagents();
}
public Collection<Reagents> getReagents(){
ArrayList<Reagents> result = new ArrayList<Reagents>();
result.add(new Reagents("testovaci reagencie",11));
return result;
}
}
In your JSP do this:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#page import="cz.dbReagents.web.reagentsWeb.ReagentsWeb"%>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Reagencie</title>
</head>
<body>
<jsp:include page="../WEB-INF/menu/menu.jsp"/>
Reagents
<%
for(Reagent r : reagents){
out.println(r.getName());
}
%>
</body>
</html>

Related

javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver

I am trying to run simple code to connect with mysql. I m using eclipse and tomcat.
I have added mysql-connector-java-8.0.1.9.jar in Libraries:
Libraries-->mysql-->mysql-connector-java-8.0.1.9.jar
I have been at it for several hours adding and removing mysql-connector-java-8.0.1.9.jar to build path cannot get it running. Please help. Do I need to add any thing in run configuration??
This is the error on eclipse browser:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:599)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:500)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:917)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:846)
org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:129)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
compute.DB.con(DB.java:21)
org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
compute.DB.con(DB.java:21)
org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
This is the output in console:
> Mar 16, 2020 4:57:27 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/Mysql] has started
Mar 16, 2020 4:57:27 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/Mysql] is completed
Mar 16, 2020 4:57:41 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Mysql] threw exception [javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver] with root cause
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
at compute.DB.con(DB.java:21)
at org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1195)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)
Try - 05 Trying to establish con in regCheck.jsp
Con method called Try 5A
Try 05a - Trying to connect
This is DB.java:
package compute;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.*;
public class DB {
final static String DB_URL = "jdbc:mysql://localhost:3306/new";
final static String USER = "admin";
final static String PASS = "admin";
public static Connection con() {
System.out.println("Con method called Try 5A");
Connection con;
try {
System.out.println("Try 05a - Trying to connect");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Try 05a- con established ");
return con;
}catch(Exception e) {
System.out.println(e);
return null;
}
}
public static Statement st() {
Statement stmt;
try {
stmt=DB.con().createStatement();
return null;
}catch(Exception e) {
System.out.println(e);
return null;
}
}
}
This is regCheck.jsp:
<%#page import="java.sql.Connection" %>
<%#page import="compute.DB" %>
<%#page import="java.sql.PreparedStatement" %>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String user = request.getParameter("username");
System.out.println("Try - 05 Trying to establish con in regCheck.jsp");
Connection con = DB.con();
System.out.println("Preparing Statement in regCheck.jsp");
PreparedStatement p = con.prepareStatement("Insert into register(username)values(?)");
p.setString(1,user);
System.out.println("user::" +user);
int R = p.executeUpdate();
if(R!=0)
response.sendRedirect("index.html");
else
response.sendRedirect("register.jsp");
%>
</body>
</html>
The JDBC driver library is missing from your runtime CLASSPATH i.e. the CLASSPATH where Tomcat looks for the required classes in order to execute your compiled code.
I added mysql-connector-java-8.0.19.jar to Tomcat 7.0/Lib and now it's working find.
I removed it from Library and everywhere and it works.
I created new project with the same code and without adding mysql-connector-java-8.0.19.jar to any library or build path, it's running fine.
I found the instruction to add to tomcat somewhere on youtube.

Unable to compile the class for JSP

We are facing the below exception when I click on View button to see the user details :
2017-06-30 19:23:52,831 ERROR [com.myapp.jsp] - <Unable to compile class for JSP:
An error occurred at line: 53 in the jsp file: /WEB-INF/jsps/ViewUserDetails.jsp
apache cannot be resolved or is not a field
50: </myapphtml:myappRow>
51:
52: <myapphtml:myappRow align="left" label="userdetails.field.label.orgs">
53: <logic:iterate name="userDetails" property="Orgs" id="org">
54: <bean:write name="org" property="name" /><br>
55: </logic:iterate>
56: </myapphtml:myappRow>
Stacktrace:>
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 53 in the jsp file: /WEB-INF/jsps/ViewUserDetails.jsp
apache cannot be resolved or is not a field
50: </myapphtml:myappRow>
51:
52: <myapphtml:myappRow align="left" label="userdetails.field.label.orgs">
53: <logic:iterate name="userDetails" property="mappedOrgs" id="org">
54: <bean:write name="org" property="name" /><br>
55: </logic:iterate>
56: </myapphtml:myappRow>
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:490)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:743)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:485)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:410)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:337)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
at org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
at org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:318)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at at com.myapp.tools.auth.client.AuthFilter.doFilter(AuthFilter.java:512)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.myapp.tools.auth.client.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:90)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
This happens only in the combination of RHEL 7.x and Tomcat combination.
I am not able to reproduce this issue in combination other then this.
Attaching the ViewUserDetails.jsp
<%# page import="org.apache.struts.Globals" %>
<%# page import="com.myapp.tools.api.impl.User" %>
<%# include file="include/commonDef.jspf" %>
<tiles:insert definition="myapp.csd.office.layout.default">
<tiles:put name="header" type="String">
<myapphtml:myappPageHeaderTab headerImage="images/default/icn_user.gif" headerText="form.page.title.user_details"/>
</tiles:put>
<tiles:put name="content" type="String">
<myapphtml:myappBlock>
<myapphtml:myappMessage
genErrorKey="<%= myappGlobals.GENERAL_ERROR %>"
valErrorKey="<%= Globals.ERROR_KEY %>"
genErrorHeading="MC.General.genError"
valErrorHeading="MC.General.genError"
headingBundle="myappBASETAG"/>
</myapphtml:myappBlock>
<strutshtml:form action="/processUserList.do" method="POST">
<input type="hidden" name="action" value="" />
<myapphtml:myappRow align="left" label="userdetails.field.label.orgs">
<logic:iterate name="userDetails" property="Orgs" id="org">
<bean:write name="org" property="name" /><br>
</logic:iterate>
</myapphtml:myappRow>
<myapphtml:myappButtonRow formname="UserDetailsForm">
<myapphtml:myappButton action="cancel" name="form.button.label.cancel" buttonStyle="button_gt" />
</myapphtml:myappButtonRow>
</strutshtml:form>
</tiles:put>
</tiles:insert>
Try to see if you have a String called org change it to other name as org1.
See answer.
String org = ""; something like this in jsp somewhere on the top of
that package statement. Change that variable name to org1 or some
other meaningful one and your problem will be solved.
There's a known issue in Tomcat which its Compiler is interpreting this as a reference to the field apache of object org
The code works in TC4 because the generated code is much simpler - it
ignores a number of possible error conditions. The fix for these
issues in TC5 and TC6 includes a fully qualified reference to
org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode()
The complier is interpreting this as a reference to the field apache
of object org.
When you have generated code, there is always going to be the
possibility of a naming clash. I can't see an easy way around this.
I am going to mark this as WONTFIX. More generally, avoiding the use
of widely used package prefixes (org, com, etc) as variable names will
reduce your risk of hitting this issue.
As an aside, a quick Google suggests you might also want to avoid 2
letter variable names that clash with top level domains.
Notice that RHEL uses a specific tomcat version which not as community version.
Any community releases of Tomcat are not supported
The supported releases of Tomcat are included in our products:
Tomcat rpm package included in RHEL 7.1, which is based on Tomcat
7.0.54.
Please note that we backport critical bug and security fixes on the
above versions when issues are raised or CVEs are reported. Therefore,
the versions we provide are not exactly the same as community versions
of Tomcat.

Error org.apache.jasper.JasperException: java.lang.ClassNotFoundException

I have tried to make a simple custom tag JSP application, bu talternatively I keep receiving this error, or sometimes Unable to load tag handler class "HelloTag" for tag "ex:Hello".
My index.jsp looks like this:
<%#page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>customised tag</title>
</head>
<body>
<ex:Hello/>
</body>
</html>
My custom.tld from WEB-INF looks like this:
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Example TLD</short-name>
<tag>
<name>Hello</name>
<tag-class>HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
And this is the Java class HelloTag.java from WEB-INF/classes:
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.write("Hello Custom Tag!");
}
}
The entire error log:
exception
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.labJavaIJ.web.index_jsp
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:177)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:376)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.ClassNotFoundException: org.apache.jsp.labJavaIJ.web.index_jsp
java.net.URLClassLoader.findClass(URLClassLoader.java:381)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:132)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:172)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:376)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Calling java function in jsp page

I am having a java file named FaceDetection.java that has a function named FaceDetectionMain and that take two arguments .The java file dont have any main function.Now i call this java class file function in my jsp page like this :
FaceDetection.FaceDetectionMain(imagepath,imagename);
But am getting an exception as follow :
Apr 21, 2014 10:44:23 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/SharedCrpto1] threw exception [Unable to compile class for JSP:
An error occurred at line: 90 in the jsp file: /uploadwebcamimage.jsp
FaceDetection cannot be resolved
87: fos.write(imageData);
88: String imagepath=filesstore+"\\"+imagename+".png";
89:
90: Boolean flag=FaceDetection.FaceDetectionMain(imagepath,imagename);
91: %>
92: </body>
93: </html>
Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 90 in the jsp file: /uploadwebcamimage.jsp
FaceDetection cannot be resolved
87: fos.write(imageData);
88: String imagepath=filesstore+"\\"+imagename+".png";
89:
90: Boolean flag=FaceDetection.FaceDetectionMain(imagepath,imagename);
91: %>
92: </body>
93: </html>
Please help me remove this exception.
Full stackTrace for exception is :
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:97)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Using the default package isn't a good practice, and often causes problems such as the one you are having (you can't import the default package, so no need to try the page import directives) Try putting your class in a package and import it, you should find that it works properly.

Insertion of illegal Element

This is my JSP page. On button click, I am calling this JSP page which will convert the HTML document to a PDF document.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.io.*,com.itextpdf.text.*,com.itextpdf.tool.xml.*,com.itextpdf.text.pdf.PdfWriter"%>
<!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>
<body>
<%
//step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("C:/Documents and Settings/preetish/Desktop/practice/Buttons.html"));
//step 5
document.close();
%>
</body>
</html>
But when I am running index.jsp on an Apache Tomcat Server, it is giving me the following error log:
SEVERE: Servlet.service() for servlet [jsp] in context with path [/paginanation] threw exception [An exception occurred processing JSP page /pfd.jsp at line 19
16: // step 3
17: document.open();
18: // step 4
19: XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("C:/Documents and Settings/preetish/Desktop/practice/Buttons.html"));
20: //step 5
21: document.close();
22:
Stacktrace:] with root cause
java.lang.ClassCastException: Insertion of illegal Element: 30
at com.itextpdf.text.Phrase.add(Phrase.java:367)
at com.itextpdf.text.Paragraph.add(Paragraph.java:345)
at com.itextpdf.tool.xml.html.Div.end(Div.java:117)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:189)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:206)
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:141)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:174)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:223)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:185)
at org.apache.jsp.pfd_jsp._jspService(pfd_jsp.java:76)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
You have forgotten to import Document class.
<% page import="class">
This is the way to import in jsp page
You yourself are using loose HTML4 (in the JSP at least), and are trying to convert XHTML.
In general: make a standalone application (library) to have an easier development.
Turn the original HTML4 page into XHTML, and validate the page (Tiny Validator, any browser plugin.)
The error aludes probably an illegal element, like
<b><p>lorem</p><p>ipsum</p></b>
A p-tag is not allowed inside a b-tag.
As you know XHTML needs singletons to be written as <img .../>. For HTML you could leave a space before />. An open tag <p> needs a clöosing tag </p>.

Categories