Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ouput text to console from Servlet

I have a servlet. But it is not working as desired. Hence for debugging purposes, I want to print statements to the java console(the one that can be opened using the java icon in taskbar). However, if I use System.out.println("message"), it doesnt display in java console.

Is there any alternative way where I can display messages to the console from the servlet? Or can anyone suggest me an alternative way to display messages to any other console?

like image 280
mithun1538 Avatar asked Mar 20 '10 20:03

mithun1538


People also ask

How to write response in servlet?

Use the PrintWriter object given by getWriter to deliver character data (). Use a ServletOutputStream and manually control the character sections to blend binary and text data, for example, to generate a multipart response. The setCharacterEncoding(java. lang.

Can we use system out Println in servlet?

out. println() Since the System object is part of the core Java objects, it can be used everywhere without the need to install any extra classes. This includes Servlets, JSP, RMI, EJB's, ordinary Beans and classes, and standalone applications.

What is HttpServletRequest and HttpServletResponse?

The HttpServletRequest object can be used to retrieve incoming HTTP request headers and form data. The HttpServletResponse object can be used to set the HTTP response headers (e.g., content-type) and the response message body.

How do you invoke a servlet?

Calling a Servlet Programmatically To include another servlet's output, use the include() method from the RequestDispatcher interface. This method calls a servlet by its URI and waits for it to return before continuing to process the interaction. The include() method can be called multiple times within a given servlet.


2 Answers

In which console do you expect it to appear?

Depending on the servlet container (I assume Tomcat), the logs are stored in a logs folder. For Tomcat this is tomcat/logs (or more often referred to as CATALINA_HOME/logs). If you are running it from within an IDE - they should be in the IDE console.

As a sidenote, using System.out isn't advisable for a real product - use a logging framework (like log4j).

like image 172
Bozho Avatar answered Nov 07 '22 09:11

Bozho


Servlet (HttpServlet) has a method log(String s) inherited from GenericServlet class.

So you can just include

log("output text")

in the servlet's code and see output.

If you use Eclipse log goes right into console.

If you use IntellijIdea log goes into Run --> "Tomcat Localhost Log" tab.

like image 31
Andrew Avatar answered Nov 07 '22 08:11

Andrew