Home Jsp Simple-jsp-example Comparing Two String with

Related Tutorials


 
 

Share on Google+Share on Google+

Comparing Two String with

Advertisement
Whenever we have to check that the value entered by the user is fulfilling the condition or not, then in these condition the tag is used. This tag will not work if the value entered by the user doesn't match any of the condition given in the progra

Comparing Two String with <c:if>

        

Whenever we have to check that the value entered by the user is fulfilling the condition or not, then in these condition the <c:if> tag is used. This tag will not work if the value entered by the user doesn't match any of the condition given in the program. In this tag there is no way to specify the default value. This tag is just like the if statement in java. This tag is not for applicable if we want to one thing if the condition is true, and another thing if the condition is false. In this situation this tag will become obsolete. 

To make a program on this, it is important to use the following tld's in the lib folder. i.e. /WEB-INF/lib folder, first is jstl.jar and the second one is standard.jar. We have made a program on if statement using jstl library, in which we are going to print "Welcome to roseindia.net".

In this program we are making  use of <c:if>, in which we are going to check whether the given string is equal to another string, we are not taking the case of a string into consideration. In this program we are making use of <c:set var = str value= "rOsEiNdIa"> tag. In this tag var is a must. In this tag the value is assigned to variable str.  The value of the variable str cannot be accessed outside the tag, so assign the variable str to the id of useBean <jsp:useBean id="str" type="java.lang.String" />.  At last from this id to check the condition whether the given string is equal to the another string or not. The test attribute of the <c: if> tag checks the condition. 

The code of the program is given below:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>If Caseless</title>
</head>
<body>
<c:set var="str" value="rOsEiNdIa" />
<jsp:useBean id="str" type="java.lang.String" />
<c:if test='<%=str.equalsIgnoreCase("roseindia")%>'> <strong>They are
equal</strong></c:if>
</body>
</html>

The output of the program is given below:

Download this program.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 12, 2008

Related Tutorials

Discuss: Comparing Two String with   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com