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

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()中排队[方法ArrayBlockingQueue],并且参数是从常规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)

最佳答案

好的,再次查看代码,我想我知道出了什么问题。

doGet返回时,我怀疑Tomcat期望您已完成请求和响应-因此它很可能清除了forward中所需的所有信息...并将数据返回到客户,当然。

我不认为请求/响应对设计用于servlet中的这种方式-并非没有特定的支持,即使doGet已完成,您也要指定该支持/容器,容器不应认为请求已完成。

可以这样考虑:如果您有一个空的doGet方法,那么对请求/响应以及网络连接的期望是什么?现在考虑您要做的就是启动一个新线程,该线程维护对这些对象的引用-为什么您希望容器阻止发送响应给客户端?

尚不清楚为什么首先要执行此操作,而不仅仅是在forward方法中调用doGet,但是如果您确实要将工作交给另一个线程,则需要找到一些方法告诉容器的方式。 (我知道有支持此功能的容器,但我不知道是否存在标准化的方法。)

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

07-25 21:21