views:

190

answers:

2

I'm writing my first JEE application, using EJBs, Servlet, JSPs and the likes.
I'm using JBOSS 4.2 AS and Eclipse as and IDE, including tasks like starting and stopping the server.

What I can't understand is that while logging instructions inside a jsp, like:

<% System.out.println("Log this!"); %>

log as expected, both in Eclipse console and in $SERVER_HOME/server/default/log/server.log, any kind of logging instruction I've tried inside a servlet fails.

Here's the code from the jsp that calls the servlet:

<form action="MyServlet" method="POST" accept-charset="utf-8">
    <input type="text" name="id" value="" id="id">
    <input type="submit" value="Go &rarr;">
</form>

And of course the servlet itslef:

public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public MyServlet() {
        super();
        System.out.println("Hi, I'm your servlet's constructor");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.err.println("Hi, this is your servlet on system.err");
        System.out.println("Hi, this is your servlet on system.out");
        System.out.println(request);
        ServletContext sc = getServletContext();
        sc.log("Hi, this is your servlet on servlet context!");
    }
}

Am I missing something obvious? Doing everything wrong, or just looking in the wrong place?

A: 

If you don't see anything being written to stdout/stderr, then this just means that the servlet is actually not been executed. That can have lot of trivial causes. Is the Servlet in the classpath? Is the Servlet definied in web.xml or annotated with @WebServlet? Is the url-pattern correct? Does the request URL cover it? What does the Eclipse debugger say? What does the appserver's own logs say?

By the way, this isn't actually logging. This is just plain writing to stdout/stderr. Logging is more than that.

BalusC
Thanks for the comment. The servlet *was* being executed, but somehow wasn't up to date. A full republish resulted in *all* the instruction working lake a charm.
Agos
+3  A: 

I just created a dummy servlet that I deployed on JBoss 4.2.3 and using System.out.println outputs in the Eclipse console and in server/default/log/server.log so I cannot reproduce your problem. There must be something else wrong.

That being said, I don't recommend using System.out or System.err, especially when using a Java EE container. Instead, you should use a logging framework (Don't Use System.out.println!) and I suggest to use logback (the successor of log4j).

Pascal Thivent
+1 for the useful “proper logging” suggestion.
Agos