Introduction Creating a JSP page is very simple. If you are new in JSP technology then you can go through The basics of JSP. You may also see Eclipse keyboard shortcut keys if you are not much familiar with Eclipse IDE. To write the first JSP page we need to configure Web Application Server with Eclipse. Here we will use Tomcat server and deploy our JSP single page application on Tomcat by using Eclipse.
Installation requiredFor creating JSP Web application, we need to download and install below components.
JRE and Tomcat Server configuration in Eclipse By configuring we tell Eclipse that where is the location of our Java Runtime Environment(JRE) and which Web Server to be used for running JSP application from within Eclipse. You may install and configure multiple number of Web Server. Based on your requirement you can choose the required one.
Creating a JSP Web application in Eclipse
Create an empty Java Dynamic Web Project First we will create a blank Dynamic Web Project using Eclipse and add our first "Hello World" JSP page in it. Then we will deploy it on Tomcat Web Server.
In Eclipse, go to File > Other, search for "Dynamic" > Dynamic Web Project. Select this and press next.
While pressing "Next" button we will go to the wizard. Here enter Project name and location to create this project. "Demo_JSP" in our case. Complete remaining wizard with default settings and press "Finish".
Change to 'Java' perspective set from default in Eclipse. Go to Window > Open Perspective > Other > Java, you can see the empty Web Application in 'Package Explorer' panel.
Web.xml A deployment descriptor xml file is created in WEB-INF folder by Eclipse. It is used by Tomcat server. Soe default tag is already created in web.xml containing multiple entries for landing page for our application.
So when we create a new page with one of these names, and deploy our web project on tomcat, it will automatically know which page will be used as a home page.
Creaing a new JSP Page To write our first JSP page, go to File > New > Other and enter 'JSP' and select 'JSP File'. Put a file name 'index.jsp' on the Wizard and select 'Demp_JSP' project. After that select 'New JSP File' html template. Once completed, a new index.jsp page will be created in WebContent folder. If no created go inside the folder.
Open index.jsp page and put below code snippet inside HTML body tags.
<%-- This is a JSP Comment before JSP Scriplet --%>
//It will show in Console
System.out.println("Hello World will display in Console!");
//It will show as a HTML page
This above code contains a JSP comment, and a JSP Scriptlet. This code will print Hello World! in two different output streams. One is to Console and another one is to Response output stream.
You can find more info about JSP Technology specification on Oracle Website.
JSP Web Application deployment on Tomcat Now its time to deploy and run the Web Project. Right click on 'Demo_JSP' project name in 'Package Explorer' panel within Eclipse.
From Project context select Run As > Run on Server. This will launch a new wizard 'Run on Server' and all our configured servers including Tomcat server will appears in the list, Select and click to Finish.
You check 'Always use the server when running this project to overcome unnecessary steps to follow.
When you click on Finish, it will start Tomcat server, and runs our application on it. Eclipse will automatically launch the application in it's internal browser. URL is, http://localhost:8080/Demo_JSP/
You can also copy paste this link to any other installed Web browser and they will show the same 'Hello World' output as well.