JSP – Hello World Tutorial
In this tutorial, we provide a step-by-step guide for creating a hello world web application using JSP technology.
- Eclipse IDE (Mars release)
- Java 1.7
- Apache tomcat 7
1. Create dynamic web project
Open eclipse, then select File -> New -> Dynamic 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.
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:
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:
Right click on the pages folder, then select New -> Other
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:
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’:
In the next screen, select ‘Deployment Assembly’ tab, then add ‘pages’ folder through the ‘Add’ button.
Now, you would be able to access helloWorld.jsp using the following url:
3. Deploy the applicaton
Follow this guide in order to setup Tomcat 7 in eclipse.
Then deploy your application to Tomcat 7, just drag and drop the application to the tomcat instance under servers view.
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:
In order to find the port number of the application, double click on the tomcat instance in servers view:
These port attributes are editable and you can change them anytime.
/HelloWorld-JSP: denotes the deployed application name.
/helloWorld.jsp: denotes the name of the requested JSP page.
Finally, the output of the url would look like: