How to make my first JSP page?

In this tutorial you will learn how to make your first JSP page?

How to make my first JSP page?

Tutorial of creating your first JSP Page

In this tutorial you will learn how to create your first page and then run on the Tomcat Server. Tomcat server is one of the most popular Servlet Container for deploying and running the Java based application. In this tutorial you will learn how to make your web application and run on Tomcat Server.

How to create First JSP Page?

Java Server Pages is simple text file with .jsp extension and it contains HTML code along with embedded Java Code. JSP file is compiled into Servlet and then run on the Tomcat. Tomcat server automatically compiles the JSP file into Servlet.

Adding Java code into JSP Page

Three types of tags can be used to add the code into JSP page. These are:

  • JSP Scriptlet
  • JSP expression
  • JSP declaration

In the first tutorial we will use the JSP expression  for adding the Java code.

Syntax of JSP Expressions is:

 <%="Any thing" %>

Here is the first JSP code:

<html>
<head>
<title>First JSP page.</title>
</head>
<body>
<p align="center"><font color="#FF0000" size="6"><%="Java Developers Paradise"%></font></p>
<p align="center"><font color="#800000" size="6"><%="Hello JSP"%> </font></p>
</body>
</html>

Create the above file and save as "index.jsp" file. Next step is to create the web application and then deploy on the Tomcat Server.

Above example code display the "Java Developers Paradise" and "Hello JSP" on the browser. You can click on the following URL to run the example online on our server.

Execute the example.

To deploy and run the JSP application we need the Tomcat server. If you don't know how to download and install Tomcat please check the tutorial "How to install and Configure the Tomcat Server?".

To deploy the web application on Tomcat Server you can use any one of the following techniques:

  • Create war file and deploy on Tomcat manually
  • Use the Eclipse for creating web application and then automatically running on Tomcat
  • Create war file from Eclipse and then deploy on Tomcat

Let me explain you how to create war file manually and deploy on Tomcat

Go to any directory of your hard disk and then create a directory "firstjsp", in my computer we are using the "D:\examples\roseindia.net\webapp\firstjsp" directory.

Now create the following directory Structure:

Directory Structure of Web Application
 /firstjsp/
   index.jsp
  WEB-INF
   web.xml
   classes
   lib

You should save the "index.jsp" file into the root of the web application as shown above.

You should add the following code in the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>First JSP</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Now to build the war file you should use the following command:

Go to the directory "D:\examples\roseindia.net\webapp\firstjsp" and then use the following command to create the war file:

jar -cvf ../firstjsp.war *

Here is the output of the above command:

D:\examples\roseindia.net\webapp\firstjsp>jar -cvf ../firstjsp.war *
added manifest
adding: index.jsp(in = 253) (out= 156)(deflated 38%)
adding: WEB-INF/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/classes/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/lib/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/web.xml(in = 487) (out= 247)(deflated 49%)

D:\examples\roseindia.net\webapp\firstjsp>

Now to deploy the application copy the firstjsp.war into the webapps directory of the Tomcat and then start the Tomcat.

Now open the browser and type "http://localhost:8080/firstjsp/" it should display the following output on the screen:

First JSP Example

Download the Source code and the war file of the application discussed here. 

Check more tutorials of JSP at our JSP Tutorials Home Page.