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" %>

 

Expression Language Functions with examples

                                           EL Functions
 Introduction:

1.    The main objective of Expression Language (EL) is to separate java code from the Jsp.
2.    If we have any business functionality we can separate it into a java class and we can invoke that functionality through Expression Language (EL) syntax.
3.    3.     The page designer has to know only function name and tld   file  uri to get its functionality.  Hence EL is almost treated as alternative to custom tags.

Designing Expression Language (EL) function application contains the following steps.

1.    Writing a java classes with required functionality.
2.    Writing tld  file to map java class to the Jsp.
3.    Write a taglib directive to make business functionality available to Jsp.
4.    Write Expression Language (EL) function call.

1. Writing a Java Class:

Any java class can simply acts as a repository for EL functions.
The only requirements of a method that acts as EL function  it should be declared as public & static.
Method can take parameters also.
No restrictions on return type void return types also allow.

2.Writing tld file: (tag library descriptor)

For Expression Language  functions tld  file provides mapping between  Jsp [where functionality is required] and java class [where functionality is avialble].tld file is an  xml file.
We can configure Expression Language (EL) function by using function tag in this tld.  This tag  defines the following four child tags.
1.    <description> 
2.    <name>
a.    By means of this name only we can call EL functionality in the Jsp.
3.    <function-class>
4.    It defines fully qualified name of java class name where Expression Language (EL) function is available.
5.    <function-signature>
Signature of  the method .

Example:

<function>
          <name>upper</name>
          <function-class>StrMethods</function-class>
          <function-signature>java.lang.String upper(java.lang.String)</function-signature>
 </function>


3.Writing tagLib Directive:

This is to make EL functionality available to the Jsp.
<%@ taglib prefix="myString" uri="StringOperations" %>

4.Invoking EL Functions:

${myString:upper(param.name)}

EL Function Flow:

1.    Where Jsp engine encounters EL functions call with prefix and EL function name then it checks for corresponding taglib directive with matched prefix.
2.    From taglib directive Jsp engine identifies uri and checks for tld file with matched uri.
3.    From the tld file Jsp engine checks for corresponding class and required method.
4.    Jsp engine executes that method and return its result to the Jsp.
  
Example: TestELServlet.java

Package com.myjavahub;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class TestELServlet extends HttpServlet {
          public void doGet(HttpServletRequest request,HttpServletResponse response)
                   throws ServletException, IOException {
         
           RequestDispatcher view = request.getRequestDispatcher("result.jsp");
           view.forward(request,response);
           }
  }

result.jsp

<%@ taglib prefix="myString" uri="StringOperations" %>
<html>
<body>
<h1 align="CENTER">Result</h1>
 ${myString:upper(param.name)}
</body></html>

index.html

<html>
<body>
<h1 align="CENTER">Test Expression Language</h1><br><br>
<form method="GET" action="MyTestEL.do">
Name: <input type="text" name="name"/><br>
<input type="submit" name="command" value="submit"/>
</form></body></html>

My.tld

<?xml version="1.0" encoding="ISO-8859-1"?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
     version="2.5">

<tlib-version>1.2</tlib-version>
<uri>StringOperations</uri>
 <function>
          <name>upper</name>
          <function-class>StrMethods</function-class>
          <function-signature>java.lang.String upper(java.lang.String)</function-signature>
 </function>

</taglib>

my method java file is:

public class StrMethods {
          public static String upper(String s) {
                   return s.toUpperCase();
          }
 }