servlet中不能使用HttpClient吗?

servlet中不能使用HttpClient吗?
在servlet中使用HttpClient,包已配置好,编译通过。
package use;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import bean.Stock;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import java.util.TimerTask;
import java.util.HashMap;
import java.util.Timer;
public class UseServlet extends HttpServlet
{

private HashMap stocks;
[b] private HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager());[/b]
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
............
.........

但在网页中调用时,出现错误:
[b]javax.servlet.ServletException: Error instantiating servlet class use.UseServlet[/b]
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Unknown Source)

问题在哪?请帮忙看一下!

3个回答

我觉得不会是httpClient造成的吧

UseServlet 在你构造函数(init())都调用完毕以后,变量都初始化完,都没有异常,肯定
[code="java"]private HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager()); [/code]
这句话是没有错的。

你自己也说是网页调用的时候错误,你看看下面有没有cause by什么?

丢出异常还是要完整一点


[code="java"]private HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager()); [/code]
换成
[code="java"]private HttpClient httpclient;
{
try {
httpclient = new HttpClient(new MultiThreadedHttpConnectionManager());
} catch (IOException e) {
e.printStackTrace();
}
}[/code]

然后把所有的异常贴出来看看。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问