开发者

Problem with the Apache DefaultHttpClient class

开发者 https://www.devze.com 2023-03-02 03:28 出处:网络
I am a newbie for servlet applications, trying to learn the subject. On my way, I wrote a servlet class called FormWebServlet that uses the org.apache.http.impl.client.DefaultHttpClient class. However

I am a newbie for servlet applications, trying to learn the subject. On my way, I wrote a servlet class called FormWebServlet that uses the org.apache.http.impl.client.DefaultHttpClient class. However, I get the exception

java.lang.ClassNotFoundException: org.apache.http.impl.client.DefaultHttpClient

... that clearly shows that this class does not exist, although I have added the jar file to the project.

The server returns an "HTTP Status 500" error with the message that the "root cause" is this missing class:

java.lang.NoClassDefFoundError: org/apache/http/impl/client/DefaultHttpClient
    testPackage.FormWebServlet.doGet(FormWebServlet.java:45)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

TRIES 1) I searched for the missing jar file and added it to the project (by going on the project in "Eclipse JAVA EE IDE for Web Developers, 20100917-0705"'s project explorer, select "Properties", selected the "Java Build Path" and clicked the [A开发者_JS百科dd External JARs...] button.) The added jar file is from the Apache site and is called httpclient-4.1.1.jar. 2) As I still get the same error, I extracted with 7-ZIP the DefaultHttpClient.class file and put it into the WebContent/WEB-INF/lib directory.

QUESTION What am I doing wrong? Neither of the other two JAR files do contain the class, nor is there a class with this name in the WEB-INF/lib folder.

DETAILS Inculded JARs:

common-httpclient-3.0.1.jar
httpclient-4.1.1.jar
httpcore-4.1.jar

FormWebServlet.jar:

/**
 * 
 */
package testPackage;

import java.io.IOException;
import java.io.PrintWriter;

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

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import coreServlets.ServletUtilities;

/**
 *
 */
@WebServlet(description = "Gets the book's barcode with a form", urlPatterns = { "/FormWebServlet" })
public class FormWebServlet extends HttpServlet {

    /** */
    private static final long serialVersionUID = 6008315960327824633L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request,
     *  HttpServletResponse response)
     */
    protected void doGet(final HttpServletRequest request, 
            final HttpServletResponse response)
    throws IOException, ServletException {
        final String BAR_CODE = request.getParameter("barCode");

        response.setContentType("text/html");
        final PrintWriter out = response.getWriter();

        if (BAR_CODE != null) {
            HttpClient client = new DefaultHttpClient();
            final String ADDRESS = ServletUtilities.getHttpAddress(BAR_CODE);

            out.println("ADDRESS = \"" + ADDRESS + '\"');
            HttpGet get = new HttpGet(ADDRESS);

            HttpResponse httpResponse = null;

// Removed commented code that will use these objects
        }
    }
}


Just put the JAR files themselves into WEB-INF/lib, not the class file. That way they will be included in your deployment.

0

精彩评论

暂无评论...
验证码 换一张
取 消