0

I'd like to test my servlet by printing the results to the console. System.out.println does not seen to work for a servlet. Does anyone know how I can achieve this? Main purpose is for debugging at a later stage.

public class GetAllStaff extends HttpServlet {

private static final long serialVersionUID = 1L;

static StaffDAO dao = new StaffDAO();
static ArrayList<Staff> sList = null;

public void doGet(HttpServletRequest request,
        HttpServletResponse response)
                throws ServletException, IOException {

    sList = dao.getAllStaff();

    for (int i = 0; i < sList.size(); i++)
    {

    }
  }
2
  • 1
    Why not just connect the debugger when you want to debug? Generally, you'd use a logger (like log4j, logback and/or slf4j) for logging. Also, because there could be multiple Servlet instances in the pool at runtime, is your dao thread safe? Commented Jan 2, 2016 at 18:40
  • @Elliot: That pool is only true when the servlet implements the since Servlet 2.4 (2003!) deprecated SingleThreadModel interface. Nonetheless, making it static is indeed fishy. Commented Jan 2, 2016 at 20:45

2 Answers 2

1

You could use

ServletContext context = getServletContext( );
context.log("This is a log item");

The logs are not printed in Eclipse console but can be found at logs folder of servlet container (say Apache Tomcat)

Reference: http://www.tutorialspoint.com/servlets/servlets-debugging.htm

Sign up to request clarification or add additional context in comments.

Comments

0

You may want to print everything on a browser with the following code?

PrintWriter out = res.getWriter();
out.println("Some information on the browser...");

P.S I tried System.out.println("something"); in my IDE (Intellij), the result showed up in the console.

1 Comment

What is res (what is the type/instantiation)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.