HTTP状态500-Servlet执行引发异常(java.lang.ClassNotFoundException) [英] HTTP Status 500 - Servlet execution threw an exception (java.lang.ClassNotFoundException)

查看:230
本文介绍了HTTP状态500-Servlet执行引发异常(java.lang.ClassNotFoundException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在找不到此资源错误,并且无法弄清楚我的代码有什么问题.这是我的代码.

I have been getting this resource not found error and I'm not able to figure out whats wrong with my code. Here is my code.

TwitterServlet.java:

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:

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:

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执行引发异常

HTTP Status 500 - Servlet execution threw an exception


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)

推荐答案

如果您使用的是pom.xml,请包括以下依赖项:

If you are using pom.xml, please include this dependency:

<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

Or include this jar in your classpath : poi-ooxml-schemas-3.10-FINAL.jar

这篇关于HTTP状态500-Servlet执行引发异常(java.lang.ClassNotFoundException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆