JSP Declaration JSP Scriptlet JSP Expression JSP Comments Creating a simple JSP Page How to run simple JSP Page Directory Structure of JSP

JSP Declaration

A declaration tag is a piece of Java code for declaring variables, methods and classes. If we declare a variable or method inside declaration tag it means that the declaration is made inside the servlet class but outside the service method. We can declare a static member, an instance variable (can declare a number or string) and methods inside the declaration tag.

Syntax of declaration tag: 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 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.

JSP Scriptlet

Scriptlet tag allows to write Java code into JSP file. JSP container moves statements in _jspservice() method while generating servlet from jsp. For each request of the client, service method of the JSP gets invoked hence the code inside the Scriptlet executes for every request. A Scriptlet contains java code that is executed every time JSP is invoked.

Syntax of Scriptlet tag: 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. 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.

JSP Expression

Expression tag evaluates the expression placed in it. It accesses the data stored in stored application. It allows create expressions like arithmetic and logical. It produces scriptless JSP page.

Syntax: Here the expression is the arithmetic or logical expression. Example: In this example, we are using expression tag 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.

JSP Comments

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: 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 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

Creating a simple JSP Page

A JSP page has an HTML body incorporated with Java code into it We are creating a simple JSP page which includes declarations, scriplets, expressions, comments tags in it.

Example: 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 guru JSP example. The number is num12num32 (1212). Today’s date is the current date

How to run simple JSP Page

JSP can be run on web servers or application servers. Here we will be using a webserver, and we can deploy it on the server enclosing it in a war application. We can create JSP in an application (war).

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:

There are two options either to choose a server or manually add the server to this application. In this case, we have already added JBoss server to the application hence, we select the existing server. Once we select the server the server option is shown in point 2 which server we want to select. There can be multiple servers configured on this application. We can select one server from all those options Once that option is selected click on finish button and application will run on that server.

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.

Directory Structure of JSP

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:

In this article, we have learnt about syntactic elements like expression tags, Scriptlet tags which simplify code in JSP. Use of JSP comments. We have created a simple JSP page and made it run on the server.