开发者

Servlet post request logging to Eclipse console

开发者 https://www.devze.com 2022-12-17 18:31 出处:网络
I\'m writing my first Java EE 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.

I'm writing my first Java EE 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?


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消