In this tutorial, we will learn how to create a new project in Eclipse for developing a dynamic JSP web application. The web application for the purpose of simplicity at this time, will be a single page JSP application that will show current date and time on the server. The application will run on Apache Tomcat 7.0.
This tutorial assumes that you have working installations of both Apache Tomcat and Eclipse IDE for Java EE development on your computer:
Step 1: Start Eclipse
Assuming that you installed Eclipse in
eclipse folder in your
C:\, go to the
C:\eclipse folder and double-click
eclipse.exe file, as shown below:
Eclipse boots up and shows the welcome screen:
Step 2: Create new project
Now, in the Eclipse IDE, click
File -> New -> Dynamic Web Project. New Dynamic Web Project window pops up as shown below:
Enter 'Project name' as "First JSP Project". Leave rest of the settings as they are and click 'Next'.
Next window asks for adding paths to folders containing pre-existing Java source code files. Since this is a new project and we don't have any other source code folders, we'll just accept the default source code folder of
src. Click 'Next' to proceed forward.
Next window asks for web module configuration settings. A web module is a small JSP application that can either be run as a stand-alone application or it can be a component of a much larger enterprise application. An enterprise application can contain one or web modules along with other modules.
We accept the default 'Context root' and 'Content directory' folder names and leave the 'Generate web.xml deployment descriptor' option unchecked. We click 'Finish' and let Eclipse create folders, files and structure for new JSP application.
Step 3: Examining the project
Eclipse opens our 'First JSP Project' project in a new window.
If we expand the 'First JSP Project' node in the 'Project Explorer' window, we can see the names and contents of the different folders that Eclipse has created for us:
Among all the folders that Eclipse has created for our project, the one important to us now is
Step 4: Add new JSP page
Now left-click the 'First JSP Project' node in the 'Project Explorer' window to select it. Then right-click to see the list of options. Select
New -> JSP File. 'New JSP File' window pops up as shown below:
Enter 'File name' as "index.jsp", keep the 'WebContent' folder selected and click 'Next'. 'Select JSP Template' window appears as shown below:
Select 'New JSP File (xhtml)' template from the template names in the left column and click 'Finish'.
Eclipse creates a new JSP file from the template we selected and puts in enough HTML code along with a JSP directive to give us a head-start for adding further code to this file.
Now replace code in the
index.jsp file by the code given below:
<?xml version="1.0" encoding="ISO-8859-1" ?> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>First JSP Page</title> </head> <body> <p>Current date and time: <%= new java.util.Date().toString() %></p> </body> </html>
The code is pretty much the same as it was except that I set the title of the page to "First JSP Page" and added one line of HTML intermixed with JSP code to display the current date and time on the server.
Step 5: Configuring Eclipse to use Apache Tomcat
Now that we have a JSP page and a pretty basic JSP web application, we want to run this application on a Servlet container. We have already installed Apache Tomcat 7.0 on our computer. We will now configure Eclipse to use that server as the Servlet container for our JSP application.
Run -> Run in the Eclipse window. This should make Eclipse pop up a 'Run On Server' window as shown below:
Now, click to expand 'Apache' node from the list of servers. Then select 'Tomcat v7.0 Server' from the list of options. Check the 'Always use this server when running this project' option and leave rest of the options untouched and proceed to click 'Next' as shown below:
'Tomcat Server' configuration window opens up and asks for the path to the installation folder of Apache Tomcat 7.0. Click 'Browse' to select the Tomcat installation folder on your computer and click 'Finish'.
Step 6: Running our JSP application
Eclipse configures the JSP application to use the Apache Tomcat server 7.0. Next, it automatically starts the Tomcat server in the background and opens the
index.jsp page in a new tab as shown below:
Congratulations! You just finished creating a new JSP project in Eclipse, configured Eclipse to use Apache Tomcat 7.0 with your JSP project, and then finished running your JSP application successfully for the first time.