HTTP状态500 - Servlet执行引发异常(java.lang.ClassNotFoundException)

时间:2015-06-09 04:43:56

标签: java jsp servlets twitter

我一直在找到这个资源未找到错误,我无法弄清楚我的代码有什么问题。这是我的代码。

TwitterServlet.java:

package com.dhruvil.jsw;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;




public class TwitterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public TwitterServlet() {
     super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String CONSUMER_KEY = "xxx";
        request.setCharacterEncoding("UTF-8");
        String CONSUMER_KEY_SECRET = "xxx";
        String AccessToken = "xxx";
        String AccessTokenSecret = "xxx";
        response.setContentType("text/html");
        response.setContentType("application/PDF");
        String input1 = request.getParameter("first");


        System.out.println(input1);
        if (input1 == null || input1.trim().length() == 0) {
            input1 = "You didn't enter any text.";
       }
        XSSFWorkbook workbook = new XSSFWorkbook();

        XSSFSheet sheet = workbook.createSheet("Twitter Sheet");



        try{
        Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
            AccessToken oathAccessToken = new AccessToken(AccessToken, AccessTokenSecret);
            twitter.setOAuthAccessToken(oathAccessToken);
            List<Status> status = twitter.getUserTimeline(input1);
            List<String> tweets  = new ArrayList<String>();
            for (Status status2 : status)
            {
                tweets.add(status2.getText());
                System.out.println("---Tweet---"+status2.getText());    
            }
            WriteExcel.writeExcel(tweets, "Tweets.xls");       




            }catch (TwitterException te){
                System.out.println("Error occured "+te);
            }
            System.out.println("Tweets Retrieved");



        super.doPost(request, response);
    }

}

WriteExcel.java:

package com.dhruvil.jsw;

import java.io.FileOutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class WriteExcel
{

    public static void main(String[] args)
    {

    }

    public static void writeExcel(List<String> data, String fileName)
    {
        try
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("Sheet1");
            HSSFRow rowhead = sheet.createRow((short) 0);
            rowhead.createCell((short) 0).setCellValue("CellHeadName1");
            rowhead.createCell((short) 1).setCellValue("CellHeadName2");
            rowhead.createCell((short) 2).setCellValue("CellHeadName3");
            int i = 1;
            for (String dataRow : data)
            {

                HSSFRow row = sheet.createRow((short) i);
                row.createCell((short) 0).setCellValue(dataRow);
                i++;
            }
            String yemi = "e:/"+fileName;
            FileOutputStream fileOut = new FileOutputStream(yemi);
            workbook.write(fileOut);
            fileOut.close();
        }
        catch (Exception e1)
        {
            e1.printStackTrace();
        }
    }
}

的index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>
<form action="TwitterServlet" method="get">
Enter Twitter Search Details    : <input type="text" name="first"><br>
<input type="submit">
</form>
</body>
</html>

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>
    Twitter17</display-name>
    <servlet>
        <description>
        </description>
        <display-name>
        TwitterServlet</display-name>
        <servlet-name>TwitterServlet</servlet-name>
        <servlet-class>
        com.dhruvil.jsw.TwitterServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TwitterServlet</servlet-name>
        <url-pattern>/TwitterServlet</url-pattern>
  </servlet-mapping>
     <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>

错误:

  

HTTP状态500 - Servlet执行引发异常

type Exception report

message Servlet execution threw an exception

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

exception 

javax.servlet.ServletException: Servlet execution threw an exception


root cause 

java.lang.NoClassDefFoundError: org/openxmlformats/schemas/spreadsheetml/x2006/main/CTSheet
    com.dhruvil.jsw.TwitterServlet.doGet(TwitterServlet.java:46)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


root cause 

java.lang.ClassNotFoundException: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    com.dhruvil.jsw.TwitterServlet.doGet(TwitterServlet.java:46)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

2 个答案:

答案 0 :(得分:1)

你缺少你的依赖关系中任何与poi相关的jar,比如poi-ooxml-schemas-3.10-FINAL.jar

查看以下链接了解更多详情: -

Common POI Related Errors

答案 1 :(得分:0)

如果您使用pom.xml,请包含此依赖关系:

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml-schemas</artifactId>
  <version>3.10-FINAL</version>
</dependency>

或者在您的类路径中包含此jar:poi-ooxml-schemas-3.10-FINAL.jar