Writing the First JSP Page

In this tutorial, we will show you how to develop the first JSP page that outputs an important message: Hello World to the web browser.

If you do not have Java IDE available, we are highly recommended to install NetBeans IDE for practicing and learning JSP.

First, launch the NetBeans IDE.

Then, create new Web Application project by following the steps below:

From the File menu choose New Project... menu item
From the File menu choose New Project… menu item
JSP - Java Web App
Choose Java Web > Web Application
JSP - Name and Location
Enter the name of the project: HelloWorld and Location where you want to store project file. We are using C:\JWeb
JSP - Server and Settings
Choose the GlassFish Server so that you can execute the JSP. If you have Apache Tomcat installed, you can click Add.. button to use Apache Tomcat instead. We will use GlassFish server for the sake of simplicity.
JSP - Frameworks
We are not going to use any framework so just click finish to create HelloWorld project
JSP - HelloWorld
NetBeans generates the project skeleton for you with a JSP page: index.jsp

Next, replace the code in the index.jsp file by the following code snippet:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1><% out.println("Hello World!");%></h1>
    </body>
</html>
 
Finally, click the Run Project button to run the index.jsp file.

 

JSP - Run Project
JSP – Run Project

The following screenshot is the output when you run the JSP page in the web browser. It displays the message “Hello World” as a standard heading  H1 of the HTML page.

JSP - Hello World - Output
JSP – Hello World – Output

The index.jsp page is composed of HTML and Java code. The Java code is embedded inside the HTML document between notations <% and %>.

<% out.println("Hello World!");%>
Code language: HTML, XML (xml)

In JSP, The code snippet above is known as Scriptlet. Inside the scriptlet block, we used the method  println() of the  out object to print the messasge "Hello World".

Congratulation! you’ve developed the first JSP page and launch it in the web browser to get the output.