1

我不确定此代码段有什么问题:

public class RequestProcessor extends Thread{
    private ServletContext context;
    private HttpServletRequest request;
    private HttpServletResponse response;

    public RequestProcessor(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) {
        this.context = servletContext;
        this.request = request;
        this.response = response;
        handler = HandlerFactory.getFactory(request);

        start();            
    }

    public void run(){
        RequestDispatcher dispatcher = context.getRequestDispatcher("/index.jsp");

        try {
            dispatcher.forward(request, response);
        } catch (ServletException e) {
        } catch (IOException e) {
        }
    }   

这些线程enqueue()在 a 中排队 [method],ArrayBlockingQueue参数取自 normal HttpServlet

因此,来自 HttpServlet 的 doGet() :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    scheduler.enqueue(getServletContext(), request, response);
}

我有一个 Java 异常,转发有问题。

我的代码有什么问题?

编辑:

看看这个堆栈跟踪:

Servlet.service() for servlet RequestDispatcher threw exception
java.lang.NullPointerException
    at org.apache.catalina.connector.Request.notifyAttributeAssigned(Request.java:1563)
    at org.apache.catalina.connector.Request.access$000(Request.java:107)
    at org.apache.catalina.connector.Request$3.set(Request.java:3369)
    at org.apache.catalina.connector.Request.setAttribute(Request.java:1515)
    at org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:542)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:286)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at source.RequestProcessor.run(RequestProcessor.java:42)
Exception in thread "Thread-3" java.lang.NullPointerException
    at org.apache.catalina.connector.Request.notifyAttributeAssigned(Request.java:1563)
    at org.apache.catalina.connector.Request.access$000(Request.java:107)
    at org.apache.catalina.connector.Request$3.set(Request.java:3369)
    at org.apache.catalina.connector.Request.setAttribute(Request.java:1515)
    at org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:542)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:286)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at source.RequestProcessor.run(RequestProcessor.java:42)
4

1 回答 1

3

好吧,再看代码,我想我明白了哪里出了问题。

doGet返回时,我怀疑 Tomcat 期望您完成请求和响应 - 所以它很可能会清除forward...内所需的各种信息,以及将数据返回给客户端,当然。

我不相信请求/响应对被设计为在 servlet 中以这种方式使用 - 如果没有特定支持,您可以指定即使doGet已完成,容器也不应认为请求已完成。

可以这样想:如果您有一个 doGet方法,您希望请求/响应和网络连接会发生什么?现在考虑您所做的只是启动一个新线程来维护对这些对象的引用 - 为什么您希望容器推迟向客户端发送响应?

目前还不清楚为什么你首先要这样做,而不仅仅是forwarddoGet方法中调用,但是如果你确实想把工作交给另一个线程,你需要找到一些方法来告诉容器关于它。(我知道有容器支持这一点,但我不知道是否有标准化的方法。)

顺便说一句,我还建议实施Runnable而不是扩展Thread,并使用执行程序服务,以便可以重用线程。

于 2013-06-25T06:05:25.847 回答