0% found this document useful (0 votes)
31 views6 pages

06 JSP CustomTags

The document discusses JavaServer Pages (JSP) Expression Language (EL) and custom tags. It provides examples of using EL to access and manipulate variables, perform arithmetic operations and conditional checks. It also demonstrates using JSTL tags and custom tags to iterate and loop through collections, select options, and perform math operations. The examples show how to access implicit objects, scopes and attributes in EL expressions to output dynamic content in JSPs.

Uploaded by

nehaagrawal2210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

06 JSP CustomTags

The document discusses JavaServer Pages (JSP) Expression Language (EL) and custom tags. It provides examples of using EL to access and manipulate variables, perform arithmetic operations and conditional checks. It also demonstrates using JSTL tags and custom tags to iterate and loop through collections, select options, and perform math operations. The examples show how to access implicit objects, scopes and attributes in EL expressions to output dynamic content in JSPs.

Uploaded by

nehaagrawal2210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Deccansoft Software Services Adv. Java / JSP 2.

0 Custom Tags
-------------------------------------------------------------simpleTagDemo.jsp----------------------------------------------------
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

<html>
<body>
<h2>
Output from tag file:
<p>
<tags:simpleTagFile/>
</p>
</h2>
</body>
</html>
----------------------------------------------\WEB-INF\tags\simpleTagFile.tag-------------------------------------------------
Hello...How are you?

-------------------------------------------------------panelDemo.jsp----------------------------------------------------------------
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<html>
<body>
<h2>Mobile Phone Portal: </h2>
<table border="0">
<tr valign="top">
<td>
<tags:panel color="#aaaaff" bgcolor="#ddddff" title="Nokia Models">
6600<br>
7610<br>
6681<br>
</tags:panel>
</td>
<td>
<tags:panel color="#00fc00" bgcolor="#c0ffc0" title="Samsung">
X-100<br>
D-600<br>
E-700<br>
</tags:panel>
</td>
</tr>
</table>
</body>
</html>
---------------------------------------------------------panel.tag---------------------------------------------------------------
<%@attribute name="color" %>
<%@attribute name="bgcolor" %>
<%@attribute name="title" %>

<table border="1" bgcolor="${color}">


<tr>
<td><b>${title}</b></td>
</tr>
<tr>
<td bgcolor="${bgcolor}">
<jsp:doBody/>
</td>
</tr>
</table>

1
Deccansoft Software Services Adv. Java / JSP 2.0 Custom Tags
-------------------------------------------------------------------------------------------------------------------------------
The JSP Expression Language (EL) is an expression syntax that lets you manipulate collections and other
complex data structures from within JSP tags.

The EL is a language inspired by JavaScript, and to some extent, XPath (a language used to access pieces
of an XML document), but the EL is much more forgiving about variables without a value (null) and
performs more data-type conversions automatically. These features are important for a web application,
where the input is mostly in the form of request parameters. The parameters may be present in some
requests but not in others, and the browser always sends request parameters as text values, while the
application often needs to use them as numbers or Boolean values (true or false). The way the EL is
designed, you rarely need to worry about absent values or type conversion at all.

An EL expression contains variables and operators. Any bean stored in one of the JSP scopes (page,
request, session, or application) can be used as an EL variable. In addition, the EL supports the following
implicit (predefined) variables:

Variable name Description


pageScope A collection (a java.util.Map) of all page scope variables.
requestScope A collection (a java.util.Map) of all request scope variables.
sessionScope A collection (a java.util.Map) of all session scope variables.
applicationScope A collection (a java.util.Map) of all application scope variables.

Param A collection (a java.util.Map) of all request parameter values as a single String


value per parameter.
paramValues A collection (a java.util.Map) of all request parameter values as a String array
per parameter.
Header A collection (a java.util.Map) of all request header values as a single String
value per header.
headerValues A collection (a java.util.Map) of all request header values as a String array per
header.
Cookie A collection (a java.util.Map) of all request cookie values as a single
javax.servlet.http.Cookie value per cookie.
initParam A collection (a java.util.Map) of all application initialization parameter values as
a single String value per parameter.
pageContext An instance of the javax.servlet.jsp.PageContext class, providing access to
various request data.

2
Deccansoft Software Services Adv. Java / JSP 2.0 Custom Tags
-------------------------------------------------------------------------------------------------------------------------------

Operators describe what you want to do with the variables. The operators you can use in an EL
expression probably look familiar to you if you've used any programming language before, because they
are the same as those supported by most languages:

Operator Description
. Access a bean property or Map entry.
[] Access an array or List element.
() Group a subexpression to change the evaluation order.
? : Conditional test: condition ? ifTrue : ifFalse.
+ Addition.
- Subtraction or negation of a value.
* Multiplication.
/ or div Division.
% or mod Modulo (remainder).
== or eq Test for equality.
!= or ne Test for inequality.
< or lt Test for less than.
> or gt Test for greater than.
<= or le Test for less than or equal.
>= or ge Test for greater than or equal.
&& or and Test for logical AND.
|| or or Test for logical OR.
! or not Unary Boolean complement.
empty Test for an empty variable value: null, an empty String, or an array, Map, or Collection
without entries).
func(args) A function call, where func is the function name and args is zero, one or more comma-
separated function arguments.

An EL expression can also include literals: numbers, text (within single or double quotes), Boolean
values and null.

Because an EL expression can appear in many places where static text can also appear, you must tell the
JSP container that it should treat an EL expression as such. You do this using delimiters. An EL
expression always starts with the ${ delimiter (a dollar sign and a left curly brace) and ends with } (a
right curly brace). Here's an EL expression that adds 5 to a variable named amount:

${amount + 5}

3
Deccansoft Software Services Adv. Java / JSP 2.0 Custom Tags
------------------------------------------------------------------ELDemo.jsp----------------------------------------------------
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<c:set var = "x" value = "8" scope="page" />
<c:set var = "y" value = "7" scope="request" />
<html>
<body>
<h1>
${ x + y }
</h1>
</body>
</html>
-----------------------------------------------------------------ForEachDemo.jsp----------------------------------------------
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String str [] = { "C" , "C++" , "Java" };
pageContext.setAttribute ( "myArray" , str ) ;
%>
<html>
<body>
<h1>
<c:forEach begin="1" end="10" var="i" step="2">
${i} <br>
</c:forEach>
<c:forEach var="s" items="${myArray}">
<li> ${ s }
</c:forEach>
</h1>
</body>
</html>
--------------------------------------------------------SelectDemo.jsp-----------------------------------------------------------
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
pageContext.setAttribute ("cities", new String[]{"Bombay","Calcutta","Hyderabad","Madras","New Delhi"});
%>
<html>
<head>
<script language="javascript">
function city_onchange()
{
document.forms[0].submit();
}
</script>
</head>
<body>
<h1>
<form action='selectDemo.jsp' method='post'>
Select a city :
<select name="city" onchange="city_onchange()">
<c:forEach var="city" items="${cities}">
<option value="${city}" ${param.city == city?'selected':''}> ${city}
</c:forEach>
</select>
<input type='submit' value='Proceed'>
</form>
${ ! empty param.city ? ‘You selected’ : ‘’} ${ param.city }
</h1>
</body>
</html>

4
Deccansoft Software Services Adv. Java / JSP 2.0 Custom Tags
-----------------------------------------------------------------MathDemo.jsp---------------------------------------------------
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="/WEB-INF/eldemo-taglib.tld" prefix="my" %>
<html>
<body>
<h1>
<form method="post">
Num 1 : <input type="text" name="num1" value="${param.num1}"> <br>
Num 2 : <input type="text" name="num2" value="${param.num2}"> <br> <br>
<input type="submit" name="submit" value="+">
<input type="submit" name="submit" value="-">
<input type="submit" name="submit" value="*">
<input type="submit" name="submit" value="/">
<input type="submit" name="submit" value="%">
</form>
<c:if test="${ !empty (param.submit) }">
${param.num1}
${param.submit}
${param.num2}
=
${ my:perform ( param.num1 , param.num2 , param.submit )}
</c:if>
</h1>
</body>
</html>
-------------------------------------------------- WEB-INF/tlds/SimpleTagLibrary.tld------------------------------------
<taglib
version="2.0" 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-jsptaglibrary_2_0.xsd">

<description>A tag library exercising JSP 2.0 features</description>


<tlib-version>1.0</tlib-version>
<short-name>demo</short-name>
<uri>/SimpleTagLibrary</uri>

<function>
<description>Concatenate Strings</description>
<name>concat</name>
<function-class>eldemo.MyELMethods</function-class>
<function-signature>
java.lang.String concat(java.lang.String,java.lang.String)
</function-signature>
</function>

<function>
<description>Arthimetic Operations</description>
<name>perform</name>
<function-class>eldemo.MyELMethods</function-class>
<function-signature>
java.lang.String perform(java.lang.String, java.lang.String, java.lang.String)
</function-signature>
</function>

</taglib>

5
Deccansoft Software Services Adv. Java / JSP 2.0 Custom Tags
-------------------------------------------------- MyELMethods.java-----------------------------------------------
package eldemo;

public class MyELMethods


{

public static String concat(String str1, String str2)


{
return str1 + str2;
}

public static String perform(String num1,String num2,String operator)


{
String result = null;

double n1 = Double.parseDouble(num1);
double n2 = Double.parseDouble(num2);

if(operator.equals("+"))
result = String.valueOf(n1+n2);

else if(operator.equals("-"))
result = String.valueOf(n1-n2);

else if(operator.equals("*"))
result = String.valueOf(n1*n2);

else if(operator.equals("/"))
result = String.valueOf(n1/n2);

else if(operator.equals("%"))
result = String.valueOf(n1%n2);

return result;
}

}
-------------------------------------------------------------------------------------------------------------------------------
boolean contains( java.lang.String, java.lang.String)
boolean containsIgnoreCase( java.lang.String, java.lang.String)
boolean endsWith( java.lang.String, java.lang.String)
int indexOf( java.lang.String, java.lang.String)
java.lang.String join( java.lang.String[], java.lang.String)
int length( java.lang.Object)
java.lang.String replace( java.lang.String, java.lang.String, java.lang.String)
java.lang.String[] split( java.lang.String, java.lang.String)
boolean startsWith( java.lang.String, java.lang.String)
java.lang.String substring( java.lang.String, int, int)
java.lang.String substringAfter( java.lang.String, java.lang.String)
java.lang.String substringBefore( java.lang.String, java.lang.String)
java.lang.String toLowerCase( java.lang.String)
java.lang.String toUpperCase( java.lang.String)
java.lang.String trim( java.lang.String)

You might also like