Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with the Apache DefaultHttpClient class
    text
    copied!<p>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</p> <pre><code>java.lang.ClassNotFoundException: org.apache.http.impl.client.DefaultHttpClient </code></pre> <p>... that clearly shows that this class does not exist, although I have added the jar file to the project.</p> <p>The server returns an "HTTP Status 500" error with the message that the "root cause" is this missing class:</p> <pre><code>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) </code></pre> <p>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 [Add 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.</p> <p>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.</p> <p>DETAILS Inculded JARs:</p> <pre><code>common-httpclient-3.0.1.jar httpclient-4.1.1.jar httpcore-4.1.jar </code></pre> <p>FormWebServlet.jar:</p> <pre><code>/** * */ 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 } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload