JSP SCRIPTING ELEMENTS

Expression tag ( <%= %>)

  • This tag is used to sent the output of the expression to the output stream like method println().
  • This tag allows the developer to embed any Java expression and is short for out.println().
  • This tag starts with <%= and ends with %>.
  • A semicolon ( ; ) does not appear at the end of the code inside the tag.
For example,to show the current date and time. <HTML>
<HEAD>
<TITLE>MCA</TITLE>
</HEAD>
<BODY>
Date : <%= new java.util.Date() %>
</BODY>
</HTML>

Declaration tag ( <%! %> )

  • This tag allows the developer to declare variables or methods.
  • Before the declaration you must have <%!
  • At the end of the declaration, the developer must have %>
  • Code placed in this tag must end in a semicolon ( ; ).
  • Declarations do not generate output so are used with JSP expressions or scriptlets.
For Example, <%! String course=”Master of Computer Application”; %> <HTML>
<HEAD>
<TITLE>MCA</TITLE>
</HEAD>
<BODY>
I am pursuing <%=course%>
</BODY>
</HTML>

Scriptlet tag ( <% ... %> )

  • Between <% and %> tags,any valid Java code is called a Scriptlet. This code can access any variable or bean declared.
  • Any code in scriptlet is executed when user asks to jsp page.
It is used to
  • Executing logic at server side
  • Implementing conditional HTML by posing a conditional on the execution of portion of page.
  • Printing HTML portion repeatedly by placing them into JSP Scriptlet’s Loop
For example <HTML>
<HEAD>
<TITLE>MCA</TITLE>
</HEAD>
<BODY>
<%
for(int i=0;i<5;i++){
%>
<br>This is jsp demo</br>
<%
}
%>
</BODY>
</HTML> OUTPUT of this JSP page is This is jsp demo This is jsp demo This is jsp demo This is jsp demo This is jsp demo

Directive tag ( <%@ directive ... %>)

  • A JSP directive gives special information about the page to the JSP Engine.
  • There are three main types of directives:
    • page - processing information for this page.
    • Include - files to be included.
    • Tag library - tag library to be used in this page.
Directives do not produce any visible output when the page is requested but change the way the JSP Engine processes the page. For example, you can make session data unavailable to a page by setting a page directive (session) to false.

No comments:

Popular Posts