JSP Scripting element : Scriptlet

Scriptlet

Definition:
Scriptlet is similar to the Expression without the equal sign "=". You can insert any plain Java code inside the scriptlet. Because of mixing between Java code and HTML is difficult to maintain so scriptlet is not recommended to use anymore. Here is the syntax of the scriptlet:

 Syntax :

<%      java code   %>

Description:

1.    JSP Scriptlets begins with <% and ends %>
2.    JSP Engine places these code in the _jspService() method
3.    A scriptlet doesn't contribute any HTML.
4.    Every statement inside scriptlet should ends with semicolon.
5.    By itself a scriptlet does not generate HTML.  If a scriptlet wants to generate HTML, it can use a variable called "out".here “out “ is a implicit object we will discuss later.
      Ex:   out.println(“<table><tr><td>”);
        Out.println(“MYJAVAHUB</td></tr></table>”);

Scriptlet hit count example :

Write a jsp to display hit count of the jsp ?

Test.jsp

<%!
    int count=0;
 %>
 <%
    count ++;
    out.println("<h2>The hit count is : :"+count+"</h2>");
 %>

Output:










jstl General Purpose Tags: c:set

<c: set>                                           
2. <c: set>
The <c:set> tag is JSTL-friendly version of the setProperty action. The tag is helpful because it evaluates an expression and uses the results to set a value of a JavaBeans or a java.util.Map object.

Attributes :

The attributes of <c: set> are

1.     Value      :    Information to save
2.    Target     :    Name of the variable whose property should be modified
3.     Property :    Property to modify
4.    var           :    Name of the variable to store information.
5.     scope       :    Scope of variable to store information

Form1: To set an attribute in any scope.

Syntax:

<c:set  var = “name of attribute” value = “value of attribute” scope = “session”/>

Note : var and value attributes are mandatory but scope attribute is optional.
default scope is page.

test.jsp

<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="x" value="10" scope="session"></c:set>
<c:set var="y" value="20" scope="session"></c:set>
<c:set var='z' value="50" scope="session"></c:set>
<h3>Addition :<c:out value="${x+y+z}"></c:out></h3>
<h3>subtraction  :<c:out value="${z-y-x}"></c:out></h3>
<h3>multiplication  :<c:out value="${x*y*z}"></c:out></h3>

jstl General Purpose Tags: c:out


General Purpose Tags:
1.    <c: out>

    One of the general purpose core library tag is <c: out>. The main function of the this tag is to display the output to the user. It works like expression tag in jsp <%= ---%>.
    Form1:
1.    <c:out value = “MYJAVAHUB”/>
It prints MYJAVAHUB  in to the Jsp.

2.    <c: out  value = “${param.uname}”/>
It prints the value of request parameter uname of the Jsp.

    Form2:

<c:out value = “${param.uname}”  default = “Java Jobs”/>
If the main value is not available  or  it evaluates to null then default value will be considered.
    
Attributes:

<c: out> defines the following 3 attributes.
1.    value: It is the mandatory attribute and it is for providing the value.It should be either literal (or) runtime expression.
2.    default: It is optional attribute and it is to provide default value.
Jsp engine considered its value if and only if the value attributes evaluates to null.
3.    Escape xml: True if the tag should escape special XML characters

test.jsp
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1> WELCOME TO :::
<c:out value="${param.uname}" default="MYJAVAHUB "></c:out>
</h1>

jstl-core_Library-Introduction

Core Library
Installing JSTL :
By default JSTL functionality is not available to the Jsp.  We can provide JSTL functionality by placing the following jar files in web applications lib folder.

1.    jstl.jar :  Defines several API classes which are defined by sun people.
2.    standard.jar :   Provides Library implementation classes by vendor.

It is recommended to place these two jar in tomcat lib folder for application level use.To make core Library available to the Jsp we have to declare taglib directive as follows.

Syntax:
        
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>

1.    General purpose tags
·        <c:out>
·          <c:set>
·          <c:remove>
·           <c:catch>
2.    Conditional tags
·        <c:if>
·         <c:choose>
·         <c:when>
·         <c:otherwise>
3.    Iteration Tags
·        <c: forEach>
·        <c: forTokens>
4.    URL Related Tags
·        <c:import>
·        <c:uri>
·        <c: redirect>
·        <c:param>



Jsp Standard Tag Library -JSTL-INTRODUCTION


                            JSTL-INTRODUCTION 

Introduction :
1.    Sun people encapsulated the core functionality which is common to many web applications in the form of JSTL.
2.    Programmer can views this predefined Library without writing on his own.
3.    The main objective of ExpressionLanguage is removing java code from the Jsp.  But it fails to replace java code which processes some functionality like setting attribute in a particular scope.We can fill this gap by using JSTL.  Hence the main object of JSTL is also removing the java code from the Jsp.
4.    Vendors develop this implementation.  It is api specification.Entire JSTL Library divided in to 5 sub parts.
1.    Core Library
2.    Xml Library
3.    Formatting tags
4.    Sql Library
5.    JSTL Functions
JSTL Description:
Core Library:  
The core group of tags is the most frequently used JSTL tags. Following is the syntax to include JSTL Core library in your JSP:
Syntax:

<%@ taglib prefix="c” uri="http://java.sun.com/jsp/jstl/core" %>

It defines server standard actions to perform programming general stuff like implementing loop and conditional statements.  It can also perform Jsp fundamental task like setting attributes, writing output, redirecting the request to other pages etc.
  
Xml Library: 
The JSTL XML tags provide a JSP-centric way of creating and manipulating XML documents. Following is the syntax to include JSTL XML library in your JSP.
The JSTL XML tag library has custom tags for interacting with XML data. This includes parsing XML, transforming XML data, and flow control based on XPath expressions.
Syntax:

<%@ taglib prefix="x"  uri="http://java.sun.com/jsp/jstl/xml" %>

Before you proceed with the examples, you would need to copy following two XML and XPath related libraries into your <Tomcat Installation Directory>\lib:
XercesImpl.jar:  Download it from http://www.apache.org/dist/xerces/j/
xalan.jar: Download it from http://xml.apache.org/xalan-j/index.html
It defines several standard actions which can be used for writing, and formatting xml data.

Sql Library:

It defines several standard actions which can be used for data base  operation      
Syntax:

<%@ taglib prefix="sql"   uri="http://java.sun.com/jsp/jstl/sql" %>


JSTL Functions:  

It defines several standard actions which can be used for manipulating collections & string objects.
Syntax:

<%@ taglib prefix="fn"   uri="http://java.sun.com/jsp/jstl/functions" %>

Formatting tags:

The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Web sites. Following is the syntax to include Formatting library in your JSP:

 Syntax:

<%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt" %>