To work with JSP and make dynamic pages, you will require an environment where you can develop and run the web application that is created using JSP.
An environment is a set of software and tools needed to create an application, make changes, test them, and deployed them before reaching a live website.
In this tutorial, we will learn about creating and setting up the Environment to get started with JSP Programming.
How to Set Up the Environment?
For a beginner, I am going to tell you about How to set up the environment and start creating web applications from scratch. Following are the things you will need:
- JDK(Java Development Kit)
- Java Application Server
- IDE for Java
JDK(Java Development Kit)
JDK is a development environment for building applications using Java Programming language. It includes tools useful for developing and testing programs.
Download JDK https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html. After Downloading JDK, it will ask you to install JRE(Java Runtime Environment) to install it. Now You have to set up some Environment variables in your OS.
- Go to My Computer, Right Click on it. Click Properties.
- Click on Advanced System Setting
- Click on Environment Variable.
- In The System Variable, Click on Path and edit it.
- Add the java installation path to the path variable like “C:\Program Files\jdk1.8.0_20\bin” (it should be a double quote)
- Click OK and Ok.
- Just restart your PC
- Now Open cmd prompt and type “Java version” (for checking whether it is installed)
- It will return the Java Version else you need to follow these steps again.
Why do we set Environment variables?
Environment variables are the global variables accessed by processes under OS.
- The PATH is set so that our OS is able to access exe files from the command line.
- When we compile a java code we need javac.exe inside the bin directory where JDK is installed (for ex. C:\Program Files\Java\jdk1.8.0_91\bin).
- If we don’t set the PATH in Environment variable every time you compile or run you have to provide a complete path of javac.
- Once you set the PATH Environment variable you can compile and run the program without giving complete javac path every time.
Java Application Server
In order for web applications to run, we will need a Web Server. There are different web servers available such as Tomcat Apache, Glassfish, Geronimo, etc. In our application, we will use the popular Apache Tomcat Server.
- Get the Apache Server from https://tomcat.apache.org/download-90.cgi and Download it.
- Unzip the file into some Location.
- Now click on the Setup, Click next on the setup.
- Then, Click I agree with the license agreement.
- Check the Component button and click Next.
- Enter username and password(make it easy so that you can remember it) and click Next.
- Now see whether the path for the JRE is right and then click Next.
- Click Install and wait till the installation completes.
- At last, Click the Finish button. (You can uncheck both the options here which are Read me files and Run apache)
Let us verify its installation.
- Open your web browser
- Type in http://localhost:8080
- If you are able to see the apache tomcat page then the installation is successful.
IDE is an Integrated Development Environment in which java programs are going to be executed. There are many IDE’s available like IntelliJ IDEA, NetBeans, Eclipse, etc. which are open source. Here we are going to use Eclipse. You can get Eclipse from http://www.eclipse.org.
- Download as per your OS(32 bit or 64 bit).
- Unzip the file into some location after downloading it.
- Go to that folder and double click on eclipse.exe.
- Now Eclipse will prompt and ask you the workspace folder(Where all your files will be saved ).
- It will take time and Eclipse will start.
How to Connect Eclipse IDE with Apache Server?
- Open Eclipse, there is a server tab at the bottom.
- Click on the link to create a server.
- Select the right server from the options given(for now Apache Tomcat).
- Click Next and give the installation path for tomcat (navigate the location till tomcat ) where it is installed.
- Click Finish.
- Now you are able to see the server name Just right click on it. You can now start it, stop it, delete it as per your choice.
What is the Error Faced by Users?
When you start a server, it is most probable you will face the following error:” starting Tomcat v(any) Server at localhost has encountered a problem”.
- Just double click on the link below the server tab at the bottom.
- Now one tab will open, just check the Ports tab and change the port number of HTTP/1.1 to any(I have used 8088).
- Now, click save or do Ctrl+S.
- Now, Once again right-click on the link of the server and start it. It will get started.
Create a Simple JSP Application
- Open Eclipse
- Click on File then click New
- Click on Dynamic web project
- Enter the project name (for now OurFirstJSPProgram)
- Click Next, Next Finish.
- Go to our project name.
- Expand it.
- Right-click on Web Content.
- Click the New>JSP file>write the name of the JSP file.
- Click Next and Click Finish.
Write the following code in JSP Page(FirstJSPPage.jsp) and run it on Server. Let’s see the example of JSP for Current Date & time in JSP where we are using the special tags of JSP. We will learn about these tags in the coming tutorial.
<%@ 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>Insert title here</title> <%@page import="java.util.Date"%> </head> <body> <% out.println("<h1>Our First JSP Page</h1>"); out.println("<br><i>Current Date & Time is</i>"); %> <%=new Date() %> </body> </html>
Thus this is How we create a simple JSP Page.
In the next tutorial, we will discuss the concept of Life Cycle in JSP in detail in the next article of the tutorial i.e Life Cycle of JSP.