Writing values to console log using System.out.println in Java

Home > Search
  by

System.out.println is used to display text on the console and to also write events to the JVMs log.


Servlet

For example, let's say you've the following servlet. Notice the doGet method contains System.out.println("Hello World");

package com.main;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public HelloWorld() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      System.out.println("Hello World");
	}
}

 

When running this servlet in Eclipse, "Hello World" is displayed on the Console tab.

 

Likewise, "Hello World" will be written to the JVMs log.

[1/7/19 5:46:28:501 CST] 00000253 SystemOut  O Hello World

 


Class and JSP

In this example, a class named "People" contains System.out.println("Hello World");

package com.main;

public class People {
	  public People(){
		System.out.println("Hello World");
	  }
}

 

In the <head> of a JSP page, the People class is imported.

<%@page import="com.sample.main.People"%>

 

In the <body> of the JSP page, a new people object named foo is created. The text "People" must match the name of the class being imported. Using foo, get the value of the name variable. Be careful to use "out.print" and not "out.println". 

<%
  People foo = new People();
%>

 

When going to the JSP page, "Hello World" will be printed to the console.

 

Likewise, "Hello World" will be written to the JVMs log.

[1/7/19 5:46:28:501 CST] 00000253 SystemOut  O Hello World


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments