How to create First JSP Page?

How to create First JSP Page?

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:

JSP Scriptlet
JSP expression
JSP declaration
In the section we will use the JSP expression for adding the Java code.
Here is the first JSP code:

<html>

<head>

<title>my First JSP page.</title>

</head>

<body>

<p align="center"><font color="#FF00ff" size="6"><%="hello java"%></font></p>

<p align="center"><font color="#800ff" 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 "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
/myjsp/
    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>my 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\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 *

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\javanote\webapp\myjsp>

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.
  

Read More →
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!