JSP Example in Eclipse

In this article, I'm going to show you how to create a step-by-step JSP Hello World example using Eclipse IDE.

What is a JSP File?

A JavaServer Page (JSP) is a web page template 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.

A JSP file is simply an HTML page with some Java code sprinkled in, providing dynamic content on your page.

What is JSP File

The end result is an HTML page with content generated by Java code.

Where is the JSP Processed?

  • JSP files are processed on the server, for example, on web servers or application servers like Tomcat, Glassfish, or JBoss.
  • After processing, the Java code results are included in the HTML returned to the browser.

Where to Place JSP File?

  • When creating a new Eclipse dynamic project, place the JSP file in the WebContent folder.
  • The JSP file must have a .jsp extension.
  • In enterprise J2EE web applications, it is recommended to keep JSP files inside the WEB-INF folder.

Where to Place JSP File

JSP Hello World Example using Eclipse

Let's create a step-by-step JSP Hello World program using Eclipse IDE.

1. Create a Dynamic Project

  1. Open Eclipse, then select File -> New -> Dynamic Web Project.
  2. Enter the Project name as jsp-helloworld.
  3. Make sure the target runtime is set up for Apache Tomcat v8.
  4. Click Finish.

Create Dynamic Project

Here is the structure of the generated project looks like the following:

Project Structure

2. Create helloworld.jsp File

  1. In the WebContent folder, right-click and select New -> JSP File.
  2. Name the file as helloworld.jsp.
  3. Click Finish.

Add the following code to the newly created helloworld.jsp:

<%@ page import="java.time.LocalTime" %>
<%@ page import="java.time.LocalDate" %>
<html>
<body>
    <h3>Hello World of Java!</h3>
    Date and Time on Server: <%= LocalDate.now() %> <%= LocalTime.now() %>
</body>
</html>

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

3. Running the JSP Hello World Program

Right-click on the helloworld.jsp file and run it as a server. This will deploy the JSP application on the Tomcat server.

4. Demo

Once the JSP program is up and running on the Tomcat server, visit http://localhost:8080/jsp-helloworld/helloworld.jsp in your browser. The following page will be displayed:

Output Demo

We covered a very basic JSP Hello World example. We'll dig deeper into JSP in the following articles.

For more in-depth tutorials, visit JSP Tutorials.

Comments