JSP
JSP Form Processing Using getParameter()
JSP Form Processing Forms are the common method in web processing. We need to send information to...
In this tutorial, we will be learning the basic tags of JSP and how to add comments into JSP. Along with this, we will also create a JSP and run that JSP on the server.
Syntax of declaration tag:
<%! Dec var %>
Here Dec var is the method or a variable inside the declaration tag.
Example:
In this example, we are going to use the declaration tags
<%@ 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 Declaration Tag</title> </head> <body> <%! int count =10; %> <% out.println("The Number is " +count); %> </body> </html>
Explanation the code:
Code Line 10: Here we are using declaration tag for initializing a variable count to 10.
When you execute the above code you get the following output:
Output:
The variable which is declared in the declaration tag is printed as output.
Syntax of Scriptlet tag:
<% java code %>
Here <%%> tags are scriplets tag and within it, we can place java code.
Example:
In this example, we are taking Scriptlet tags which enclose java code.
<%@ 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 Scriplet</title> </head> <body> <% int num1=10; int num2=40; int num3 = num1+num2; out.println("Scriplet Number is " +num3); %> </body> </html>
Explanation of the code:
Code Line 10-14: In the Scriptlet tags where we are taking two variables num1 and num2 . Third variable num3 is taken which adds up as num1 and num2.The output is num3.
When you execute the code, you get the following output:
Output:
The output for the Scriptlet Number is 50 which is addition of num1 and num2.
Syntax:
<%= expression %>
Here the expression is the arithmetic or logical expression.
Example:
In this example, we are using expression tag
<%@ 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 Expression</title> </head> <body> <% out.println("The expression number is "); %> <% int num1=10; int num2=10; int num3 = 20; %> <%= num1*num2+num3 %> </body> </html>
Explanation of the code:
Code Line 12: Here we are using expression tags where we are using an expression by multiplying two numbers i.e. num1 and num 2 and then adding the third number i.e. num3.
When you execute the above code, you get the following output:
Output:
The expression number is 120 where we are multiplying two numbers num1 and num2 and adding that number with the third number.
Comments are the one when JSP container wants to ignore certain texts and statements.
When we want to hide certain content, then we can add that to the comments section.
Syntax:
<% -- JSP Comments %>
T his tags are used to comment in JSP and ignored by the JSP container.
<!—comment -->
This is HTML comment which is ignored by browser
Example:
In this example, we are using JSP comments
<%@ 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 Comments</title> </head> <body> <%-- Guru Comments section --%> <% out.println("This is comments example"); %> </body> </html>
Explanation of the code:
Code Line 10: Here we are adding JSP comments to the code to explain what code has. It is been ignored by the JSP container
When you execute the above code you get the following output:
Output:
We get the output that is printed in println method. Comments are ignored by container
Example:
<%@ 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 JSP Example</title> </head> <body> <%-- This is a JSP example with scriplets, comments , expressions --%> <% out.println("This is guru JSP Example"); %> <% out.println("The number is "); %> <%! int num12 = 12; int num32 = 12; %> <%= num12*num32 %> Today's date: <%= (new java.util.Date()).toLocaleString()%> </body> </html>
Explanation of the code:
Code Line 1: Here we are using directives like language, contentType and pageEncoding. Language is Java and content type is text/html with standard charset ISO 8859. Page encoding is standard charset.
Code Line 11: Here we are using JSP comments to add comments to the JSP
Code Line 14: Here we are declaring variables num12 and num32 initializing with 12.
Code Line 15: Here we are using an expression where we are multiplying two numbers num12 and num32.
Code Line 16: Here we are fetching today's date using date object.
When you execute the above code, you get the following output
Output:
We are printing overhere,
This is an application which has following directory structure, and the application has to be build.
This application has to be built, and the following message will appear after the build is successful:
After the application is built then, the application has to be run on the server.
To run JSP on the webserver, right click on the project of the IDE (eclipse used in this case) and there are many options. Select the option of run on the server. It is shown in the screenshot below;
From the diagram, following points are explained:
In the below screenshots, you can notice that our JSP program gets executed, and the test application is deployed in JBoss server marked in the red box.
In directory structure, there is a root folder which has folder WEB-INF, which has all configuration files and library files.
JSP files are outside WEB-INF folder
Directory structure of JSP
Example:
In this example there is test application which has folder structure has following:
Summary:
JSP Form Processing Forms are the common method in web processing. We need to send information to...
What is JSP LifeCycle? JSP Life Cycle is defined as translation of JSP Page into servlet as a JSP...
In this tutorial, we are going develop sample programs with JSP and using MVC architecture....
What is JSP Exception? Exceptions occur when there is an error in the code either by the developer...
What is JSP Filter? Filters are used for filtering functionality of the Java web application. They...
Download PDF 1) Explain JSP and tell its uses. JSP stands for Java Server Pages. It is a...