Image of JSP – Hello World Tutorial

ADVERTISEMENT

Table of Contents

Introduction

In this tutorial, we provide a step-by-step guide for creating a hello world web application using JSP technology.

Prerequisites:

  • Eclipse IDE (Mars release)
  • Java 1.7
  • Apache tomcat 7

1. Create dynamic web project

Open eclipse, then select File -> New -> Dynamic Web Project.

Web Project

In the next screen, fill the mandatory fields as above, you are free to choose any servlet version you want. In case you didn’t previously link apache tomcat to eclipse, then just click on “New Runtime” button and link your tomcat, in this tutorial i use Apache tomcat 7.

Click “Next”.
Web Project

Click “Next”.

Web Project

in the final screen, make sure to check ‘Generate web.xml deployment descriptor’ checkbox, so that eclipse generates automatically the web.xml file under WEB-INF.

Here we go, the structure of the generated project looks like the following:

Hello World JSP

2. Create Hello World JSP file

JSP files should be created under WebContent.

In order to organize our JSP pages, we create a new folder called ‘pages’ under WebContent so that we store all the JSP pages of the application in one place.

The new structure of the project looks like the following:

Hello World JSP

Right click on the pages folder, then select New -> Other

JSP File

Search for ‘JSP File’, then click ‘Next’ .

In the next screen, name your file as ‘helloWorld.jsp’ and click ‘Finish’.

Now that eclipse generates a template JSP file under pages. Open the file in eclipse and modify it in order to print ‘Hello World’ message:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP - Hello World Tutorial - Programmer Gate</title>
</head>
<body>
<%= "Hello World!" %>
</body>
</html>

P.S: In web applications, dynamic and static files are automatically accessed by their full path relative to WebContent without the need to define them under web.xml.

e.g. In our example, we can access helloWorld.jsp using the following url:

http://://pages/helloWorld.jsp

In order to access our jsp file without the pages prefix, we should add ‘pages’ folder under ‘Deployment Assembly’ configuration as the following:

Right click the project then select ‘Properties’:

Deployment Assembly

In the next screen, select ‘Deployment Assembly’ tab, then add ‘pages’ folder through the ‘Add’ button.

Deployment Assembly

Now, you would be able to access helloWorld.jsp using the following url:

http://://helloWorld.jsp

3. Deploy the application

Then deploy your application to Tomcat 7, just drag and drop the application to the tomcat instance under servers view.

Deploy JSP to Tomcat

Now that the application is deployed successfully under Tomcat 7, in order to start tomcat, right click on tomcat instance -> Start.

4. Access the application

After starting up tomcat, you can access the helloWorld.jsp using the following url:

http://localhost:8085/HelloWorld-JSP/helloWorld.jsp

In order to find the port number of the application, double click on the tomcat instance in servers view:

Eclipse Tomcat Port

These port attributes are editable and you can change them anytime.

/HelloWorld-JSP: denotes the deployed application name.

Hello World JSP

/helloWorld.jsp: denotes the name of the requested JSP page.

Finally, the output of the url would look like:

Hello World JSP

Summary

In this tutorial, we provide a step-by-step guide for creating a hello world web application using JSP technology.

Next Steps

If you're interested in learning more about the basics of Java, coding, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.

Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to jacob@initialcommit.io.

Final Notes