AbA2L AbA2L - 5 months ago 13
Java Question

<c:if> jstl condition does not work

i'm beginner in java ee programing and i writing a JSTL condition for verify in the session bean if Login Status is true or false.

<c:choose>
<c:if test="${Boolean.valueOf(Session.getLogStat())}">
${Session.getLogStat()}
</c:if>
<c:if test="${Boolean.valueOf(!Session.getLogStat())}">
<a href="login">login</a>
</c:if>
</c:choose>


and when i replace if with when:

<c:choose>
<c:when test="${Boolean.valueOf(Session.getLogStat())}">
${Session.getLogStat()}
</c:when>
<c:when test="${Boolean.valueOf(!Session.getLogStat())}">
<a href="login">login</a>
</c:when>
</c:choose>


i'm sorry for my bad English.


Answer

If your problem still persists ( you say in your answer : but the code does not work with ... )

I suggest that you intansiate your bean in a servlet . the servlet will set your parameter in the request . then you test what you want in your JSP .

public class ExampleServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


                Session MyObject = new Session();
                 boolean myParam=MyObject.getLogStat();  



  String s1 = Boolean.toString(myParam);


       try {


        request.setAttribute("I_WANT_TO_TEST", s1);

        RequestDispatcher view = request.getRequestDispatcher("My.jsp");
        view.forward(request, response); 


         } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }


    }}

then in your JSP

  <% String msg2=(String)request.getAttribute("I_WANT_TO_TEST");%>

        <% if ( (msg2==null || msg2.isEmpty()) ) { %> 
         // WHAT YOU ARE NOT LOGED IN             
        <% } else if(msg2!=null) { %>

         <% }  %>
Comments