简体   繁体   中英

JSTL: String Comparison issue using c:When tag

I am facing problem when comparing 2 strings values using C:When tag
I am trying as follow

<c:when test="${dbUserName eq uName}">

Where,
dbUserName = “sohail” . It is fetching value from db column
uName = “test” . This is actually input value on login page
But comparison always giving result true…

Here is complete code

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<HTML>
<body>
<c:set var='uName' value="${param.username}"/>
<c:set var='uPassword' value="${param.password}"/>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test" user="root" password="sohail"/>
<sql:query var="query1"  dataSource="${db}"
sql="select * from login where loginID = '${uName}'">
</sql:query>
<c:forEach var="row" items="${query1.rows}">
<c:set var='dbUserName' value="${row.loginID}"/>
<c:set var='dbUserPassword' value="${row.password}"/>
</c:forEach>
<c:out value="${uName}"/>
<c:out value="${row.loginID}"/>
<c:out value="${row.password}"/>
<c:choose>
<c:when test="${dbUserName eq uName}">
<c:redirect url="profile1.jsp"/>
</c:when>
<c:otherwise>
<c:redirect url="checlLogin.jsp"/>
</c:otherwise>
</c:choose>
</body> 
</html>  

Kindly suggest how can I fix comparison problem?

Hi Affe,
Thanks, you're right. The problem has been resolved
i just modified query as follow and it is working fine

<sql:query var="query1"  dataSource="${db}"
sql="select * from login">
</sql:query>  

You appear to have structured the page such that dbUserName and uName will always be the same.

<sql:query var="query1"  dataSource="${db}"
sql="select * from login where loginID = '${uName}'">

<c:set var='dbUserName' value="${row.loginID}"/>

Something would be very wrong with your database if you got back a row with a loginID that doesn't equal uName.....! (mySQL's sometimes odd default handling of case matching not withstanding.)

What is it you actually want the page to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM