jsp - undesired result in test condition while using jstl If- -


i wondering test condition jstl if-

boolean cbtest1=interests[0]!=null;//cbtest1 true 

and jsp code-

<td >interest1 <c:choose>  <c:when test="${cbtest1}"> <input type="checkbox"  name="interest1" checked/> </c:when> <c:otherwise> <input type="checkbox"  name="interest1" /> </c:otherwise> </c:choose> </td> 

but when rendering page,block of otherwise executed!

i test following-

<c:when test="${interests[0]!=null}"> 

in case otherwise executed too.

this makes me more weird when test negation-

<c:when test="${interests[0]==null}"> 

or-

<c:when test="${!cbtest1}"> 

in above both case when executed.where wrong?

value of interests[0] "y" , of cbtest true.

screenshot of page attached in value of cbtest , interest[o] ie interest1 being display.enter image description here

as jbnizt suggested- made following changes,i set array of boolean rather string array in request.-

servlet1.java

@webservlet("/serv") public class servlet1 extends httpservlet {     private static final long serialversionuid = 1l;      /**      * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)      */     protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {           string username=request.getparameter("username").trim();         string password=request.getparameter("password").trim();         system.out.println("hellow! "+username);         try{              connection con=commonutil.getconnection();             statement st=con.createstatement();                resultset rs=st.executequery("select * login");             int flag=0;             while(rs.next()){                 if(rs.getstring(1).trim().equals(username)&&rs.getstring(2).trim().equals(password)){                     flag=1;                     httpsession session=request.getsession();                     session.setattribute("loginid", rs.getstring(3).trim());                      resultset rs1=st.executequery("select * details loginid='"+ rs.getstring(3).trim()+"'");                     rs1.next();                     string name=rs1.getstring(1);                     string address=rs1.getstring(2);                     string hobby=rs1.getstring(4);                      resultset rs2=st.executequery("select * interest loginid='"+session.getattribute("loginid")+"'");                     rs2.next();                     string interest1=rs2.getstring(2);                     string interest2=rs2.getstring(3);                     string interest3=rs2.getstring(4);                      request.setattribute("name", name);                     request.setattribute("address", address);                     request.setattribute("hobby", hobby);                      boolean[] interest={(interest1!=null),(interest2!=null),(interest3!=null)};                     request.setattribute("interest", interest);                        system.out.println(interest1+interest2+interest3);                      system.out.println(name+address+hobby);                     request.getrequestdispatcher("display.jsp").forward(request, response);                     return;                 }             }             if(flag==0){                 request.setattribute("message", "invalide login credentials!");                 request.getrequestdispatcher("index.jsp").forward(request, response);                                 return;             }         }catch(exception e){             system.out.println(e);         }     }  } 

display.jsp

<%@ page language="java" contenttype="text/html"%> <%@page import="java.sql.connection" import="java.util.enumeration" import="java.sql.resultset"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>insert title here</title> </head> <body> hellow!!   <% out.println(session.getattribute("loginid")); string  name=(string)request.getattribute("name"); string  address=(string)request.getattribute("address"); string  hobby=(string)request.getattribute("hobby"); boolean[]  interests=(boolean[])request.getattribute("interest");     out.println(name+"</br>address- "+address+"</br>hobby- "+hobby+"</br>interest1- "+interests[0]+"</br>interest2- "+interests[1]+"</br>interest3- "+interests[2]+"</br>interests- "+interests); %> <form action="updateserv" method="post">   <table>     <tr >     <td >name:</td>     <td ><input type="text"  name="username" value="${name}"/></td>     </tr>      <tr>     <td> address:</td>     <td ><input type="text"  name="address" value="${address}"/></td>     </tr>    </table>     <table>      <tr >      <td >interest1        <c:choose>          <c:when test="${interests[0]}">             <input type="checkbox"  name="interest1" checked/>         </c:when>         <c:otherwise>             <input type="checkbox"  name="interest1" />         </c:otherwise>        </c:choose>      </td>      <td >interest2<input type="checkbox"  name="interest2" /></td>     <td >interest3<input type="checkbox"  name="interest3" /></td>     </tr>      <tr>      <td >hobby1<input type="radio" value="hobby1" name="hobby"/></td>     <td >hobby2<input type="radio" value="hobby2" name="hobby"/></td>     <td >hobby3<input type="radio" value="hobby3" name="hobby"/></td>     </tr>     <tr>     <td></td>     <td><input type="submit" value="update"></td>     <td></td>     </tr>    </table> </form> 

this still not working.


Comments