简体   繁体   中英

Calling java method from jsp

I am calling my java method from jsp its giving NoClassDefFoundError: error .

But my method is working when i am calling from the main method. i have used xlrd jar in my code and i have placed the jar lib folder as well. but still it gives error.

Exception trace :

org.apache.jasper.JasperException: An exception occurred processing JSP page /exportDSD.jsp at line 20

17:     //excel.getExcel();
18:     
19:     Report r = new Report();
20:     r.generateReport();
21:     
22:     System.out.println("Generated DSD");
23: %> 

Stacktrace :

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    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:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause :

javax.servlet.ServletException: java.lang.NoClassDefFoundError: xlrd/Workbook
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
    org.apache.jsp.exportDSD_jsp._jspService(exportDSD_jsp.java:107)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    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:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

My code :

import org.apache.poi.hssf.usermodel.HSSFCell; 
 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 
 import org.apache.poi.hssf.usermodel.HSSFDataFormat; 
 import org.apache.poi.hssf.usermodel.HSSFRow; 
 import org.apache.poi.hssf.usermodel.HSSFSheet; 
 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
 import xlrd.*;

 public void generateReport() throws BiffException, ClassNotFoundException, SQLException, IOException { 
 ... ... 
 } 

From jsp i am calling like this:

<% Report r =new Report(); r.generateReport(); %>

Appears JSP import not correct:

Add jar in WEB-INF/lib
Add <%@ page import="xlrd.*" %> to the top of your JSP

To import more than one classes, use the following format:

<%@ page import="com.sample.Report,xlrd.*" %>

The issue is with jar containing Workbook in some jar. Put it in the WEB-INF/lib folder. I don't think, this need to be included in the JSP as well.

NoClassDefFoundError is a JVM Error which means that JVM or class loader was not able to load the class definition . This means that class was found but due to some reason JVM or class loader was not able to load the class definition.
java.lang.NoClassDefFoundError: xlrd/Workbook doesn't mean that class was not found, if that was the case you would have got ClassNotFoundException . On contrary, class file was found but JVM is unable to load the class definition.

For NoClassDefFoundError you can check source of xlrd/Workbook if it contains some static block or member which is referencing some class which is not present or causing issue at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM