JSP Scriptlet Tag (Scripting Elements)
JSP Scriptlet Tag (Scripting Elements)
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:
o scriptlet tag
o expression tag
o declaration tag
2
Note: Do not end your statement with semicolon in case of expression tag.
3
3. <%= "Welcome "+request.getParameter("uname") %>
4. </body>
5. </html>
The jsp scriptlet tag can only declare The jsp declaration tag can declare
variables not methods. variables as well as methods.
4
6. </html>
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.
index.jsp
1. <html>
2. <body>
3. <%!
4. int cube(int n){
5. return n*n*n*;
6. }
7. %>
8. <%= "Cube of 3 is:"+cube(3) %>
9. </body>
10. </html>
5
JSP Implicit Objects
There are 9 jsp implicit objects. These objects are created by the web container that are
available to all the jsp pages.
The available implicit objects are out, request, config, session, application etc.
A list of the 9 implicit objects is given below:
Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
6
Example of out implicit object
In this example we are simply displaying date and time.
index.jsp
1. <html>
2. <body>
3. <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
4. </body>
5. </html>
Output