Http servlet没有返回任何值

时间:2016-04-15 13:04:27

标签: java servlets

当我将它与Scheduler一起使用时,HttpServletResponse没有返回任何值。在下面的代码中直到“返回”它在控制台中打印。但是下一行out.println(previousLastLine)没有返回。任何建议将不胜感激。提前谢谢。

import java.io.BufferedReader;
import java.io.FileReader;
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;

@WebServlet(value="/notifyController")
public class NotificationController extends HttpServlet{

    int rep = 0;
    String previousTime = null;
    String previousLastLine = null; 
    String lastText = null;
    int count = 0;
    boolean changeFlag = false;
    final long timeInterval = 1000;
    HttpServletResponse response = null;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        response = resp;
        if(rep == 0){
            Runnable runnable = new Runnable() {
                public void run() {
                    while (true) {
                        changeFlag = scheduleTask(); 
                        if(changeFlag == true){
                            try {
                                System.out.println("RETURNED");
                                response.setContentType("text/html");
                                PrintWriter out = response.getWriter();
                                out.println(previousLastLine);
                                out.flush();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            Thread.sleep(timeInterval);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
            rep++;
        }
    }

    public boolean scheduleTask(){
        BufferedReader br = null;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader("D:/messagedetails.txt"));
            while ((sCurrentLine = br.readLine()) != null) {
                if(lastText != sCurrentLine){
                    lastText = sCurrentLine;
                }
            }
            if(count == 1 && !previousLastLine.equalsIgnoreCase(lastText)){
                previousLastLine = lastText;
                return true;
            }
            previousLastLine = lastText;
            count = 1;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }       
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试设置响应类型并使用后台线程编写响应正文。这不适用于doPost方法。

问题是当doPost调用返回时,servlet框架将立即“提交”具有空体的响应。当后台线程的run()方法尝试在响应对象上调用setContentType时,它将抛出异常。

您正在捕获IOException,但我非常确定抛出的异常是未经检查的异常。 (如果检查日志文件,可能会在那里看到异常堆栈跟踪。)

如果您尝试实现某种异步行为,那么您需要使用符合Servlet 3.0规范的容器,并且需要以不同方式编写servlet代码。

参考: