This post explains about JSP Scriplet stay tuned for a detailed explanation of it.
JSP Scriplet is nothing Java Source Code only which will be written within <% and %> tags. So, JSP Scriplet is used to write the Java Source Code to implement business logic.
This JSP Scriplet tags are declared inside of _jspService() method. The reason JSP Scriplet declared inside of this method is that every time while generating Servlet from JSP a new object is created for each new request from the client-side.
There are 3 types of tags in JSP,
1. Declaration Tag
2. Expression Tag
3. Scriplet Tag
Each JSP Scriptet tag has their on definition to insert the code in JSP application.
Declaration Tag is used to declare variables and methods.
Expression Tag is used to display the Output of the JSP application.
Scriplet Tag is used to include Java Source Code, this is mostly used for form action pages implementation.
How to Use Scriplet tag in JSP?
JSP scriplet tag is used within <% %> tag. This will allow us to include the entire Java Source Code.
Syntax:
<% Java Source Code %>
<%
out.println("Content");
%>
Explanation:
Scriplet tag starts with <% tag and ends with %> tag.Normally to Print Java output, we write System.out.println() but in the case of JSP we only have to write out.println() and System class automatically provided by the JSP container.
In JSP there are 9 Implicit Objects are there out is one of them.
Different between Declaration and Scriplet Tag
Declaration Tag |
Scriplet Tag |
1. Declaration Tag uses <%! %> symbol to declared inside of it. |
1. Scriplet tag uses <% %> Symbol to write within it. |
2. In this we can declare variables as well as methods |
2. In this only variables can be declared not the methods. |
3. This tag is placed outside of the _jspService() method. |
3. This tag is placed inside of the _jspService() method. |
4. It creates only one object for multiple requests. |
4. It creates multiple objects for multiple requests. |
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>Insert title here</title> </head> <body> <%out.print("Hello, Scriplet Tag JSP code");%> </body> </html>
Output Hello, Scriplet Tag JSP code
Eclipse Code:
Output:
Recommended Topics..
ConversionConversion EmoticonEmoticon