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.
In this tutorial, you will learn:
- Installing Java
- How to Configure JBoss Server in Eclipse
- How to Run a JSP Dynamic Web Project in Eclipse
- How to Run a JSP Program in Eclipse
- How to Start JBoss Server and Deploy Project in Eclipse
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 – https://www.guru99.com/install-java.html
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:
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:
- Create a new server by adding name to it as localhost
- Select the version of server from the list of servers
- Server name will be added here as JBoss v5 at localhost(the one which is selected in above two steps)
- Click on the finish button to complete the steps.
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.
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.
The server has been started successfully.
Step 1) We can create a dynamic web project by right clicking on the project explorer, and we get the following window:
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.
- Name the project
- Click on finish button
Once we click on finish button, the project is created and seen on the explorer with the following folders.
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.
- Select the parent folder in the application directory
- Select the web content folder as JSP are created under that folder
- Enter the filename of the JSP.
- Click on finish button to complete the steps and JSP is created.
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:
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.
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.
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.
- Select server either choose existing server or define a new server
- Select the server which has already been defined.
- Click on the finish button.
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.
When we click the finish button, then the project is deployed on the server and message can be seen in following screen shot.
When we try to access http://localhost:8080/gurutest/guru_example.jsp
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.
This is Guru JSP text from the guru_example.jsp
- 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.