JSP HelloWorld Example using Eclipse

In this article, I'm gonna show you how to create a JSP Hello World example with Eclipse IDE.

What is a JSP File?

JavaServer page (JSP) is a template for a Web page that uses Java code to generate an HTML document dynamically. JSPs are run in a server-side component known as a JSP container, which translates them into equivalent Java servlets.

Well, a JSP file is simply an HTML page with some Java code sprinkled in and it basically gives you dynamic content that you can include on your page.
So in below diagram here, this is a basic JSP file. It has some HTML code, some Java code, you can make some more HTML code, and so on.
The end result is that you'll have an HTML page with content that's generated by some Java code.

Where is the JSP Processed?

  • JSP file is actually processed on the server. For example, JSP file can be processed on a web server or application servers like tomcat server or Glassfish or JBoss etc.
  • Finally, when JSP file processing completed, the result of the Java code is actually included in the HTML returned to the browser.
Look at the above diagram, we have a web browser, we make a request for a JSP page, it goes across, the JSP pages processed by a server and then the results of that Java code will generate HTML and that those result will actually return back to the web browser. And if returned back to the browser, just plain HTML.

Where to place JSP file?

  • When we create a new eclipse dynamic project that has a WebContent folder, we place the file there.
  • JSP file must have a .jsp extension.
  • In enterprise J2EE web applications, it is recommended to keep JSP files inside WEB-INF folder.

JSP HelloWorld Example using Eclipse

Let's create a step by step JSP hello world program using Eclipse IDE.
  1. Create Dynamic Project
  2. Create helloworld.jsp File
  3. Deploy and Running JSP HelloWorld Program
  4. Demo

1. Create Dynamic Project

Open eclipse, then select File -> New -> Dynamic Web Project.
  • Enter Project name - jsp-helloworld (Please refer below diagram)
  • Make sure that target runtime is setup for apache tomcat v8
  • Simply hit the finish button diagram here
Here we go, the structure of the generated project looks like the following:

2. Create helloworld.jsp File

Let's create a new helloworld.jsp file. So in the WebContent -> right click -> search for JSP file - name this file as helloworld.jsp - hit the finish button.
Add following code to newly created helloworld.jsp:
<%@page import="java.time.LocalTime"%>
<%@page import="java.time.LocalDate"%>

    <h3>Hello World of Java!</h3>

    Date and Time on Server : <%=LocalDate.now()%> <%=LocalTime.now()%>

Note that we have used Java 8 LocalDate and LocalTime classes to print current date and time on the server side.

3. Running JSP HelloWorld Program

Right click on helloworld.jsp file and run as the server will deploy JSP application on tomcat server.

4. Demo

Once the JSP program up and running in tomcat server, just hit http://localhost:8080/jsp-helloworld/helloworld.jsp link in a browser will display below page on the screen:

So, we covered a very basic JSP Hello World example. We're going to dig deeper or dive deeper into JSP as we go through in the following articles.