-2

I am trying the below JSP EL code for comparing input password inside a Login page.

The password variable comes from the text box and has been set appropriately; there is no error in that part (or at least it seems so!).

But when I try to compare the password with the string "admin" (the valid password). it always returns false for eq and always returns true for ne

Here is the FULL code:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Core Based Login</title>
</head>
<body>
<form action="CoreBasedLogin.jsp">
Name: <input type="text" name="name" /> <br> <br>
Password: <input type="text" name="password" /> <br> <br>
<input type="submit" value="Login"/>
</form>
<% 
String name = request.getParameter("name");
String password = request.getParameter("name");
%>
<c:set var="name" value="<%=name%>"/>
<c:set var="password" value="<%=password%>"/>
<c:if test="${password ne null}">

<c:if test="${password eq 'admin'}">
<c:out value="Login Successful"></c:out>
</c:if>

<c:if test="${password ne 'admin'}">
<c:out value="Login Failed"></c:out>
</c:if>

</c:if>
</body>
</html>

I know the == operator and eq operator both do the same thing. But I can't get this code to work.

Please advise how to proceed.

TIA :)

4
  • The syntax looks correct. What EL version are you using? Have you verified that password definitely has a value of "admin"? I would print out the value just to be sure: <c:out value="${password}"/>
    – JayS
    Sep 7, 2016 at 15:36
  • Show us the whole jsp page as well as the output of ${password}
    – jlars62
    Sep 7, 2016 at 16:11
  • @BalusC I've edited the full code in now that I had some time. But this exact code works for some of my students, doesn't work for others (and me!) Sep 8, 2016 at 15:37
  • @BalusC can we please focus on my problem and not my teaching style? Thanks. If you don't have an answer to this, I'd advise you to stop commenting. I admire and respect you. and do not want any fights.. so please.. no fights! Sep 8, 2016 at 15:46

2 Answers 2

2

In the code you have presented here, it looks to me as if you have a typo. There is the line:

String password = request.getParameter("name");

I believe name should be changed to password.

0

Without seeing more code I'm guessing that you're unable to access the password parameter in the EL context.

Try adding the following scriplet prior to your first 'if' statement. This will expose the password parameter to your EL context.

<%
pageContext.setAttribute("password", request.getParameter("password"));
%>

Take a look at section Make objects available to EL here: https://stackoverflow.com/tags/el/info

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.