Search
Free updates

Get best of Star Developer delivered right in your inbox:

Facebook
Twitter

Developing your first JSP Application in Eclipse

Step by step tutorial on how to use Eclipse to develop and run your first JSP application. You'll learn how to create a new JSP (Dynamic Web Application) project, configure Elipse to use Apache Tomcat 7.0 as the Servlet container, add a new JSP page to your project and then run your first JSP application.

Eclipse provides complete development environment for working with JSP applications. It simplifies the task of creating both dynamic (JSP pages, Servlets, JavaBeans and Java classes) and static content (CSS and JavaScript files). Built-in support for debugging, testing and deployment takes the productivity to an entirely new level.

Developing JSP applications in Eclipse
A JSP application running in Eclipse

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.

Prerequisites

This tutorial assumes that you have working installations of both Apache Tomcat and Eclipse IDE for Java EE development on your computer:

  1. Installing Apache Tomcat 7.0
  2. Installing Eclipse for JSP Development

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 folder
Eclipse folder

Eclipse boots up and shows the welcome screen:

Eclipse IDE
Eclipse IDE

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:

New Dynamic Web Project window
New Dynamic Web Project window

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.

Add source code folders
Source code folders options

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.

Configure web module settings window
Configure web module settings window

Step 3: Examining the project

Eclipse opens our 'First JSP Project' project in a new window.

First JSP Project window
First JSP Project 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:

Project Explorer window
Project Explorer window

Among all the folders that Eclipse has created for our project, the one important to us now is WebContent folder. It is this folder that should contain the web content like JSP pages, static HTML pages, CSS and JavaScript files, etc. It is this folder where we'll create our JSP file for this project.

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:

New JSP File window
New JSP File window

Enter 'File name' as "index.jsp", keep the 'WebContent' folder selected and click 'Next'. 'Select JSP Template' window appears as shown below:

Select JSP Template window
Select JSP Template window

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.

Our index.jsp JSP file in its window
JSP File window

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.

Click Run -> Run in the Eclipse window. This should make Eclipse pop up a 'Run On Server' window as shown below:

Run On Server window
Run On Server window

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:

Selecting Servlet container for our JSP application
Configuring Eclipse to use Apache Tomcat 7.0

'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'.

Selecting Tomcat installation directory
Selecting Tomcat installation directory

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:

index.jsp running on the Tomcat server
index.jsp running on the Tomcat server
You have successfully configured your JSP application to use Apache Tomcat 7.0 to run your application. From next time onwards, you can run your JSP application with a click of a button. You will not have to go through the configuration steps again.

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.

Free updates
Enter your email address to receive new tutorials, blog posts and updates by email:
Star Developer RSS Feed
Comments