2011-12-13 177 views
0

嗨我想從servlet打印列表到網頁屏幕(jsp) 我正在使用日誌,它不工作。 無論如何要做或我是否在使用這個錯誤?如何從servlet打印到網頁?

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!"); 
     } 

回答

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 

    } 
} 
+0

謝謝!有用! – 2011-12-13 21:12:45