In JSP, java code can be written inside the jsp page using the scriptlet tag. Let's see what are the scripting elements first.
JSP Scripting elements
The scripting elements provides the ability to insert java code inside the jsp. There are three types of scripting elements:
- scriptlet tag
- expression tag
- declaration tag
JSP scriptlet tag
A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:
Example of JSP scriptlet tag
In this example, we are displaying a welcome message.
Example of JSP scriptlet tag that prints the user name
In this example, we have created two files index.html and welcome.jsp. The index.html file gets the username from the user and the welcome.jsp file prints the username with the welcome message.
File: index.html
File: welcome.jsp
JSP expression tag
The code placed within JSP expression tag is written to the output stream of the response. So you need not write out.print() to write data. It is mainly used to print the values of variable or method.
Syntax of JSP expression tag
Example of JSP expression tag
In this example of jsp expression tag, we are simply displaying a welcome message.
Note: Do not end your statement with semicolon in case of expression tag.
Example of JSP expression tag that prints current time
To display the current time, we have used the getTime() method of Calendar class. The getTime() is an instance method of Calendar class, so we have called it after getting the instance of Calendar class by the getInstance() method.
index.jsp
Example of JSP expression tag that prints the user name
In this example, we are printing the username using the expression tag. The index.html file gets the username and sends the request to the welcome.jsp file, which displays the username.
File: index.jsp
File: welcome.jsp
JSP Declaration Tag
The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet.The JSP declaration tag is used to declare fields and methods.
So it doesn't get memory at each request.
Syntax of JSP declaration tag
The syntax of the declaration tag is as follows:
Difference between JSP Scriptlet tag and Declaration tag
Jsp Scriptlet Tag | Jsp Declaration Tag |
---|---|
The jsp scriptlet tag can only declare variables not methods. | The jsp declaration tag can declare variables as well as methods. |
The declaration of scriptlet tag is placed inside the _jspService() method. | The declaration of jsp declaration tag is placed outside the _jspService() method. |
Example of JSP declaration tag that declares field
In this example of JSP declaration tag, we are declaring the field and printing the value of the declared field using the jsp expression tag.
index.jsp
Example of JSP declaration tag that declares method
In this example of JSP declaration tag, we are defining the method which returns the cube of given number and calling this method from the jsp expression tag. But we can also use jsp scriptlet tag to call the declared method.
Comments
Post a Comment