javajspjsp-tags

Why are all forms displayed even if the <c:if> tags evaluate to false?


I would like to use a single jsp file for customer, supplier and shipper registration however when I run the program with <c:if> tags, all forms are shown although the System.out.print(type) shows the value "supplier".

What can I do to solve this problem?

Below I show the codes for both Supplier.jsp and Sign_in.jsp. For the registration of customers and shippers, a jsp is implemented in the same way as Supplier.jsp, modifying only the value of the type variable by the strings "customer" and "shipper". These pages will show more specific values for each of the Customer, Shipper and Supplier classes.

Supplier.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
    <title>Bienvenido!</title>
</head>
<body>
       <h2>Sign in</h2>
    <a href="Sign_in.jsp">No account yet? Sign in</a>
    
    <% request.getSession().setAttribute("type", "supplier"); %> 
    
</body>
</html>

Register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Sign in</title>
</head>
<body>
    <h1>Sign in</h1>

    <% String type = (String) request.getSession().getAttribute("type");
    String supplier = "supplier";
    String shipper = "shipper";
    String client = "client";
    %>
    
    <% System.out.println(type.equals(supplier)); %>
    
        <c:if test="${type == supplier}">
        <h2>I'm a supplier</h2>
        <form action="RegisterServlet" method="post">
            <input type="email" name="email" placeholder="Email"> 
            <input type="password" name="password" placeholder="Password">
            <button type="submit">Register</button>
        </form>
    </c:if>

    <c:if test="${type == shipper}">
        <h2>I'm a shipper</h2>
        <form action="RegisterServlet" method="post">
            <input type="email" name="email" placeholder="Email"> 
            <input type="text" name="user" placeholder="User">
            <input type="password" name="password" placeholder="Password">
            <button type="submit">Register</button>
        </form>
    </c:if>

    <c:if test="${type == client}">
        <h2>I'm a client</h2>
        <form action="RegisterServlet" method="post">
            <input type="email" name="email" placeholder="Email"> 
            <input type="text" name="user" placeholder="User">
            <input type="password" name="password" placeholder="Password">
            <input type="text" name="city" placeholder="City"> 
            <input type="text" name="address" placeholder="Address"> 
            <input type="text" name="country" placeholder="Country"> 
            <button type="submit">Register</button>
        </form>
    </c:if>
</body>
</html>

Solution

  • You need to change your test conditions to use constants as

    <c:if test="${type == 'supplier'}">
    <c:if test="${type == 'shipper'}">
    <c:if test="${type == 'client'}">
    

    because supplier, shipper, and client are local variables and not scoped attributes.

    If none of the forms render after this change, then your type attribute isn't getting set correctly. Check your hrefs to see if you are navigating to the correct JSP page or not. You are debugging Register.jsp but your links are pointing to Sign_in.jsp, so this could be another issue as well.


    @MauricePerry has made a good point that you should avoid using <% scriptlets %> since you're already using the JSTL tags. For example, your code to add a session attribute

    <% request.getSession().setAttribute("type", "supplier"); %>
    

    can be replaced with the <c:set> JSTL tag.

    <c:set var="type" value="supplier" scope="session" />
    

    The use of <% scriptlets %> is quite frowned upon in the view layer, so, do try to restrict their use to quick debugging only.