如何从servlet打印到网页?

时间:2011-12-13 20:57:04

标签: google-app-engine jsp servlets

您好我正在尝试将列表从servlet打印到网页(jsp) 我正在使用日志,它不起作用。 无论如何要做或我使用这个错误?

private static final Logger log = Logger.getLogger(TodoServiceServlet.class.getName());
.....
Todo tmp = pm.getObjectById(Todo.class, user.getEmail());
    System.out.println("user email: " + user.getEmail());

    if(tmp==null){

        log.info("You have not stored any todo lists yet");

    }else{
        System.out.println("user email is there?: " + tmp.getEmail());

        System.out.println("start printing");
        ArrayList<String> todolists = tmp.getList();
        if(todolists==null)
            System.out.println("Arraylist null");

        if(!todolists.isEmpty()){
            for(String t : todolists){
                System.out.println("In the list: " + t);
                log.info("You need to do: " + t);
            }
        }else{
            log.info("You have nothing to do chil out!");
        }

1 个答案:

答案 0 :(得分:2)

System.out将在Web服务器(通常是控制台)上打印到“标准输出”而不是屏幕。你需要做的是写HttpServletResponse

这样的事情:

public void doGet(HttpServletRequest req, HttpServletResponse resp) {
    try {
        resp.getWriter().println("user email is there?: " + tmp.getEmail());
    } catch (IOException e) {
        // handle your error here

    }
}