In this section 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 section you will learn how to make your web application and run on Tomcat Server.
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:
In the section we will use the JSP expression for adding the Java code.
Here is the first JSP code:
<title>my First JSP page.</title>
<p align="center"><font color="#FF00ff" size="6"><%="hello java"%></font></p>
<p align="center"><font color="#800ff" size="6"><%="Hello JSP"%> </font></p>
Create the above file and save as "index.jsp"
file. Next step is to create the web application and then deploy on the Tomcat
Above example code display the "hello java" 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.
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 "myjsp", in my computer we are using the "D:\examples\javanote\webapp\myjsp" directory.
Now create the following directory Structure:
Directory Structure of Web Application
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"?>
<display-name>my First JSP</display-name>
Now to build the war file you should use the following
Go to the directory "D:\examples\javanote\webapp\myjsp" and then use the following command to create the war file:
jar -cvf ../myjsp.war *
Here is the output of the above command:
D:\examples\javanote\webapp\myjsp>jar -cvf ../myjsp.war *
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%)
Now to deploy the application copy the myjsp.war into the webapps directory of the Tomcat and then start the Tomcat. now you see the output of your system.