FreeKB - Java Programming System.out.println
Java Programming - System.out.println

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

 


Bean/Class and JSP

In this example, MyClass contains System.out.println("Hello World");

package com.sample.main;

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

 

Add the following to line 1 of the JSP file. This is the combination of the package (com.sample.main) and the class (MyClass.java).

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

 

Add the following inside the <body> tags.

  • In this example, "MyClass" must be used because the name of the class is MyClass.
  • In this example, "myMethod" must be cause because the name of the method in myClass.java is myMethod.
<body>
<%
  MyClass jms = new MyClass();
  jms.myMethod();
%>
</body>

 

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 9de4c in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |