In this article, I am going to discuss JSP Elements. Please read our previous article where discussed JSP Life Cycle Phases and Methods. At the end of this article, you will understand the following pointers in detail.
- What are JSP Elements?
- JSP Directives, Actions, and Scripting Elements
- JSP Declarations with Examples
- JSP Scriptlets with Examples
- JSP Expressions with Examples
In web applications to design JSP pages we have to use the following elements:
In the web applications, JSP Directives can be used to define present JSP page characteristics, to include the target resource content into the present JSP page, and to make available user-defined tag library into the present JSP page. All the JSP directives are going to be resolved at the time of translating the JSP page to the servlet. The majority of JSP Directives will not give a direct effect to response generation.
In JSP applications, Scripting Elements are often wont to allow java code inside JSP pages but the most theme of JSP technology isn’t to permit java code inside the JSP pages. In the above context, to preserve the theme of JSP technology we’ve to eliminate scripting elements from JSP pages, for this we’ve to supply an alternate i.e. JSP Actions provided by JSP Technology. In the case of JSP Actions, we’ll define the scripting tag in situ of java code, in JSP pages, and that we will provide the respective java code inside the classes folder. In this context, when the JSP container encounters the scripting tag then the container will execute the respective java code and perform a specific action called JSP Action.
JSP Scripting Elements
In web applications, JSP Scripting Elements are often wont to provide code in JSP pages. All the JSP Scripting Elements are going to be resolved at the time of request processing. The majority of Scripting Elements will give a direct effect on response generation. JSP scripting elements enable you to insert java code directly into the servlet that will be generated from the current JSP page. There are three types of Scripting Elements:
- Declarations: Declaration of the form that is inserted into the body of the servlet class, outside of any existing methods.
- Scriptlets: Scriptlets of the form that are inserted into the servlets service method.
- Expressions: Expressions of the form that are evaluated and inserted into out.
It is often wont to provide all the java declarations like variable declarations, method definitions. Classes declarations and so on. A declaration can consist of either method declarations or variables, static constants are a good example of what to put in a declaration. The JSP you write turns into a category definition. All the scriptlets you write are placed inside one method of this class. You can also add variable and method declarations to the present class. You can then use these variables and methods from your scriptlets and expressions. We can use declarations to declare one or more variables and methods at the category level of the compiled servlet. The fact that they are declared at a class level rather than in the body of the page is significant. The class members (variables and methods) can then be employed by Java code within the remainder of the page. When you write a declaration during a JSP page, remember these rules:
- Must end the declaration with a semicolon.
- <% int i=0;%>
- We can use variables or methods that are declared in packages imported by the page directive, without declaring them in a declaration element.
- We can declare any number of variables or methods within one declaration element, as long as you end each declaration with a semicolon. The declaration must be valid in the Java programming language.
If we offer any java declarations by using-declaration scripting element then that each one java declarations are going to be available to the translated servlet as class-level declarations.
JSP Declarations Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <html> <head> <title>Declaration</title> </head> <body> <h3>--Welcome--</h3> <h3>Use of Declaration in JSP</h3> <%!int num1 = 2, num2 = 3, n = 0;%> <% n = num1 + num2 + 1; out.println("The number after adding declared variables is " + n); %> </body> </html>
The code placed in the declaration tag goes to JES class outside of the _jspService(_,_) method. Variables declared in the declaration tag becomes global variables in JES class. Variables declared in the declaration tag become a global variable in JES class. Implicit objects are not visible in the declaration tag because they are the local variable of _jspService(_,_) method, and the code placed in the declaration tag goes to the outside _jspService(_,_) method. We can also use declaration tag to place jspInit() and jspDestroy() method definition in our JSP program.
Note: The XML equivalent of <%! Code %> is:
This scripting element is often wont to provide a block of java code. It can contain any number of language statements, variable or method declarations, or expressions that are valid within the page scripting language. Within a scriptlet, you can do the following:
- On the JSP page declare variables or methods to use later.
- Write valid expressions in the page scripting language.
- Use any of the implicit objects or any object declared with the element.
- On the JSP page, write any other statement valid in the scripting language used.
- All text, HTML tags, or JSP elements you write must be outside the scriptlet.
If we offer any block of java code by using scriptlets then that code is going to be available to translated servlet inside _jspService(_,_) method. The code placed in scriptlet goes to _jspService(_,_) of JES class as it is. This code is useful as request processing logic to process the request. Variables declared in scriptlet becomes a local variable of _jspService(_,_). All implicit objects of JSPs are local variables of _jspService(_,_) method, and the code of scriptlet also goes _jspService(_,_). So, we can use implicit objects in Scriptlets. We cannot place method definitions in JSP because they become the nested method of _jspService(_,_) and java does not support nested methods.
JSP Scriptlets Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <html> <head> <title>Scriptlets</title> </head> <body> <h3>--Welcome--</h3> <h3>Use of scriptlet in JSP</h3> <% int a = 3; int b = 4; int c = 5; out.println("a is: " + a + "<br>" + "b is:" + b + "<br>" + "c is:" + c + "<br>"); out.println("Multiplication gives: " + a * b * c + "<br>"); out.println("Addition gives:" + (a + b + c)); %> </body> </html>
Scriptlets are executed at request time when the JSP container processes the request. If the scriptlet produces output, the output is stored within the out object. If you want to use the characters “%>” inside the scriptlet, enter “%\>” instead.
Note: The XML equivalent of <% Code %> is:
This is scripting element are often wont to evaluate the only java expression and display that expression resultant value onto the client browser. The expression element contains a Java expression that returns a worth. This value is then written to the HTML page. The Expression tag can contain any expression that’s valid consistent with the Java Language Specification. This includes variables, method calls then return values, or any object that contains a toString() method. It evaluates the given expression displays generated results onto browser windows. Anything that returns a result is called an expression.
Syntax: <%=Java Expression%>
If we provide any java expression in expression scripting elements then that expression will be available to translated servlet inside _JspService(_,_) method as a parameter to out.write() method. If the expression tag is used perfectly then there is no need of using out.println() on the JSP page. The code placed in the expression tag goes to the _JspService(_,_) method of JES class, so we can use implicit objects in expression tags. We can use the expression tag for instantiation and display data of the object. We can use the expression tag to call both user-defined and pre-defined methods, which return results. Here, the java expression is evaluated and then converted to String and inserted in the page. This evaluation is performed at run-time (when the page is requested), and thus has full access to the information about the request.
JSP Expressions Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <html> <body> Current Time: <%=java.util.Calendar.getInstance().getTime()%> </body> </html>
Note: XML authors can use an alternative syntax for JSP expressions:
The XML elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.
In the next article, I am going to discuss JSP Directives: Page, Include, and TagLib. Here, in this article, I try to explain the JSP Elements. I hope you enjoy this JSP Elements article.