从jsp调用java方法

时间:2015-06-02 06:31:09

标签: java jsp

我从jsp调用我的java方法,给它NoClassDefFoundError: error

但是当我使用main方法调用时,我的方法正常工作。  我在我的代码中使用了xlrd jar,我也放置了jar lib文件夹。但它仍然给出错误。

异常跟踪

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: %> 

堆栈跟踪

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)

根本原因

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)

我的代码:

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 { 
 ... ... 
 } 

从jsp我这样称呼:

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

3 个答案:

答案 0 :(得分:4)

出现JSP导入不正确:

#recents ul {
    margin-top: -10px;
    list-style-type: none;
    text-align: center;
    padding-left:0px; //Adding this will center your LI's
}

要导入多个类,请使用以下格式:

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

答案 1 :(得分:1)

问题在于某些jar中包含Workbook的jar。将它放在WEB-INF / lib文件夹中。我不认为,这也需要包含在JSP中。

答案 2 :(得分:0)

NoClassDefFoundError是一个JVM错误,这意味着JVM或类加载器无法加载类定义。这意味着找到了类,但由于某种原因,JVM或类加载器无法加载类定义。
java.lang.NoClassDefFoundError: xlrd/Workbook并不意味着没有找到课程,如果是这样的话,你会得到ClassNotFoundException。相反,找到了类文件,但JVM无法加载类定义。

对于NoClassDefFoundError,您可以检查xlrd/Workbook的来源,如果它包含一些静态块或成员,它引用了一些不存在的类或在运行时导致问题。