Problem in jsp regarding dropdown

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itgaurav198
    New Member
    • Oct 2007
    • 41

    Problem in jsp regarding dropdown

    Hi,
    I am using the following code in my JSP. Actually when i submit this jsp to a servlet then it sets the value of location attribute using request.setAttr ibute() and forwards it using RequestDispatch er and include.But when the JSP page reloads there is no effect of if-else.

    Code:
    <% if(request.getAttribute("location").equals("Chandigarh")){ %>
    <select name="Location">
    <Option value="Chandigarh" selected>Chandigarh</Option>
    <Option value="Pune">Pune</Option>
    </select>
    <%}
    else if(request.getAttribute("location").equals("Pune")){%>
    <select name="Location">
    <Option value="Pune" selected>Pune</Option>
    <Option value="Chandigarh">Chandigarh</Option>
    </select>
    <% }
    else{ %>
    <select name="Location">
    <Option value="Banglore" selected>Banglore</Option>
    <Option value="Chandigarh">Chandigarh</Option>
    </select>
    <% } %>
    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by itgaurav198
    Hi,
    I am using the following code in my JSP. Actually when i submit this jsp to a servlet then it sets the value of location attribute using request.setAttr ibute() and forwards it using RequestDispatch er and include.But when the JSP page reloads there is no effect of if-else.

    Code:
    <% if(request.getAttribute("location").equals("Chandigarh")){ %>
    <select name="Location">
    <Option value="Chandigarh" selected>Chandigarh</Option>
    <Option value="Pune">Pune</Option>
    </select>
    <%}
    else if(request.getAttribute("location").equals("Pune")){%>
    <select name="Location">
    <Option value="Pune" selected>Pune</Option>
    <Option value="Chandigarh">Chandigarh</Option>
    </select>
    <% }
    else{ %>
    <select name="Location">
    <Option value="Banglore" selected>Banglore</Option>
    <Option value="Chandigarh">Chandigarh</Option>
    </select>
    <% } %>
    Thanks
    What do you mean by "there is no effect of if-else." What actually happens when the JSP is opened from the servlet?

    Comment

    • itgaurav198
      New Member
      • Oct 2007
      • 41

      #3
      Originally posted by r035198x
      What do you mean by "there is no effect of if-else." What actually happens when the JSP is opened from the servlet?
      Hi,
      Actually I am now using the following code:
      Code:
      <% if("Chandigarh".equals(request.getAttribute("location"))) {%>
      	<select name="l" onchange="submitForm1();">
      	<Option value="Chandigarh" selected>Chandigarh</Option>
      	<Option value="Pune">Pune</Option>
      	<Option value="Banglore">Banglore</Option>
      	</select>
      	<%}
      	else if("Pune".equals(request.getAttribute("Pune"))) {%>
      	<select name="l" onchange="submitForm1();">
      	<Option value="Pune" selected>Pune</Option>
      	<Option value="Chandigarh">Chandigarh</Option>
      	<Option value="Banglore">Banglore</Option>
      	</select>
      	<%}
      	else if("Banglore".equals(request.getAttribute("location"))){%>
      	<select name="l" onchange="submitForm1();">
      	<Option value="Chandigarh">Chandigarh</Option>
      	<Option value="Pune">Pune</Option>
      	<Option value="Banglore" selected>Banglore</Option>
      	</select>
      	<%}
      	else{%>
      	<select name="l" onchange="submitForm1();">
      	<Option value="Chandigarh" selected>Chandigarh</Option>
      	<Option value="Pune">Pune</Option>
      	<Option value="Banglore">Banglore</Option>
      	</select>
      	<%}%>
      It is always showing the else block code result i.e. not depending on the value of request.getAttr ibute("location ");

      Thanks

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        request.getAttr ibute returns an Object. Typecast that to String first if the value passed is a String.
        Most probably the values are not being passed properly. Post the code in the servlet that sets the values and the code that opens the JSP.

        You can also try to print the values in the JSP to see what values are passed (if any) .

        Comment

        • itgaurav198
          New Member
          • Oct 2007
          • 41

          #5
          Originally posted by r035198x
          request.getAttr ibute returns an Object. Typecast that to String first if the value passed is a String.
          Most probably the values are not being passed properly. Post the code in the servlet that sets the values and the code that opens the JSP.

          You can also try to print the values in the JSP to see what values are passed (if any) .
          Hi,
          Thanks for your support.
          The problem has been solved.
          Actually the problem was in conditions used.

          Thanks
          Thanks a lot

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by itgaurav198
            Hi,
            Thanks for your support.
            The problem has been solved.
            Actually the problem was in conditions used.

            Thanks
            Thanks a lot
            Welcome. Always use System.out.prin tlns and out.printlns to check the values of your variables.

            Comment

            Working...