1

I tried some code to make disable-enable button by condition, but it apparently not working as I want:

<form method="get" action="reg.jsp">
    <%
        if ((String)session.getAttribute("dept") == "HR") {
    %>
        <th colspan="1"><input type="submit" value="Register"/></th>                           
    <% } 
        else {
    %> 
        <th colspan="1"><input type="submit" value="Register" disabled></th>
        <th><%= session.getAttribute("dept")%></th>
    <% 
        }
    %>
</form>

And it works like this:

button-unable

enter image description here

The "register" button supposed to be enable when the "department" is "HR".

Please help me to spot what I missed.. :(

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Hi Rani. Thanks for wanting to mark this as solved. To do so, just accept the answer below, so it has a tick mark - which I see you've done. Great! No need to add [solved] to titles - we prefer not to do that here. – halfer May 25 '16 at 20:43

1 Answers1

1

Do not use Java code in your JSP. Use JSTL tags or JSP Expression language.

How to avoid Java code in JSP files?

Your markup can be much simplified using expression language as below:

<form method="get" action="reg.jsp">
    <th colspan="1">
         <input type="submit" value="Register" ${sessionScope.dept eq 'HR' ? '' : 'disabled' }/>
    </th>  
    <th>
         ${sessionScope.dept}
    </th>
</form>
Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110