How to Run JSP File in Eclipse with JBoss Server

In this tutorial, we are going to study the basics of writing and running a JSP. We will install Java and JBoss server on our machine as they are pre-requisites to run a JSP.

Installing Java

To install java, we need to check whether we have any older versions of JDK (Java Development Kit) on the machine. If yes, uninstall them.

Refer this guide to install Java.

How to Configure JBoss Server in Eclipse

Here is a step by step process on how to configure JBoss server in Eclipse:

Step 1) JBoss server can be downloaded from the following link:

After downloading the latest version of the server from the link, we will get a zip file which needs to be unzipped.

After unzipping it, it takes to the below folder:

Configure JBoss Server

We can start and stop the server using start and stop exe from the bin folder.

Step 2) We can include the server into eclipse IDE using following way:

  1. Create a new server by adding name to it as localhost
  2. Select the version of server from the list of servers
  3. Server name will be added here as JBoss v5 at localhost(the one which is selected in above two steps)
  4. Click on the finish button to complete the steps.

Configure JBoss Server in Eclipse

Need to define which version of the server we are using, and we can specify the path from which we have unzipped above.

Once we click on finish button, then the server would be added to eclipse IDE.

Configure JBoss Server in Eclipse

Installation of JBoss has been done, and server can be accessed from here.

Step 3) Test the Installation

Check if the installation is done properly or not:

  • Once Java and JBoss server have been installed on the machine, we can start using them in the eclipse IDE.
  • In eclipse IDE, we check that there are no errors and java path has been set as an environment variable.
  • If above steps are executed successfully, then the server will start successfully.

Configure JBoss Server in Eclipse

The server has been started successfully.

How to Run a JSP Dynamic Web Project in Eclipse

Step 1) We can create a dynamic web project by right clicking on the project explorer, and we get the following window:

Run a JSP Dynamic Web Project in Eclipse

Step 2) Once we click on next button in above screen, we get the following window, where we have to enter a project name and then click on finish.

  1. Name the project
  2. Click on finish button

Run a JSP Dynamic Web Project

Once we click on finish button, the project is created and seen on the explorer with the following folders.

Run a JSP Dynamic Web Project

How to Run a JSP Program in Eclipse

Following are the steps to run a JSP program in Eclipse:

Step 1) Create a New JSP Page

In the gurutest project, right-click on the project and then click on New JSP Page.

Step 2) Select Folder and enter File name

Following window will open, and it will help to create a new JSP in the project.

  1. Select the parent folder in the application directory
  2. Select the web content folder as JSP are created under that folder
  3. Enter the filename of the JSP.
  4. Click on finish button to complete the steps and JSP is created.

Run a JSP Program

File Name given as guru_example.jsp and then clicked on next and then finish.

Step 3) Check the created JSP in the Folder

Once the JSP is created it is seen in the web content folder as below:

Run a JSP Program

Step 4) Enter the code in file and Run

Enter the following code in the file:

<%@ 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>Guru Example1</title>
</head>
<body>
<a>This is Guru JSP.</a>
</body>
</html>

Explanation of the code:

Code Line 1: Here we are defining the page directives like language which is set to java, contentType to text/html, pageEncoding to standard ISO-8859(we will be learning in more detail in JSP in Action article directives section)

Code Line 3-12: Here we have defined an html with text “This is Guru JSP” which is shown as the output.

How to Start JBoss Server and Deploy Project in Eclipse

Step 1) In the eclipse IDE, in the servers section right click on the server which has been configured and there is an option to start the server.

Start JBoss Server and Deploy Project

Step 2) Once the server is started then we can deploy the gurutest project. In the project explorer, right click on the gurutest project and then click on Run on the server and we get the following options.

  1. Select server either choose existing server or define a new server
  2. Select the server which has already been defined.
  3. Click on the finish button.

Start JBoss Server and Deploy Project

Step 3) Once you click on finish button, a new window will pop up.

There are two options,

  • Either to choose existing server or
  • Manually configure the server

In this case, we have already configured the server in the above case. Hence we can select the configured server and then click on next.

Here we get the configured projects which are to be deployed.

Start JBoss Server and Deploy Project

When we click the finish button, then the project is deployed on the server and message can be seen in following screen shot.

Start JBoss Server and Deploy Project

When we try to access http://localhost:8080/gurutest/guru_example.jsp

Where,

Localhost: Our own host on which server is started and 8080 is the port for that

Gurutest: Project which has been deployed on this localhost

Guru_example.jsp: This is JSP, which we are trying to access.

Start JBoss Server and Deploying Project

Output:

This is Guru JSP text from the guru_example.jsp

Summary

  • In this tutorial, we learned about installing Java and JBoss which are pre-requisites to run a JSP.
  • Also, we learned how to write a JSP and then deploy on the server.After deploying the project, we get the JSP output in the browser.