Problem in maintaining the selected value of dropdown list

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

    Problem in maintaining the selected value of dropdown list

    Hi,

    i have one jsp in which i select a value from drop down and on change of this a servlet is called that accesses some values and return to the same jsp where these values get populated in another dropdown list . Now the problem is when the jsp loads again (after fetching the values in servlet) the selected value of dropdown is lost. How can i make it happen that when the page reloads thedrop down list show me the previous selected value and below that another values.

    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by itgaurav198
    Hi,

    i have one jsp in which i select a value from drop down and on change of this a servlet is called that accesses some values and return to the same jsp where these values get populated in another dropdown list . Now the problem is when the jsp loads again (after fetching the values in servlet) the selected value of dropdown is lost. How can i make it happen that when the page reloads thedrop down list show me the previous selected value and below that another values.

    Thanks
    You pass the previosly selected value back to the JSP and use a simple if condition

    [CODE=java]<%
    if(value.equals ("previousValue ")) {
    %>
    <OPTION value ="<%=value%> " selected><%=val ue%> [/CODE]

    Comment

    • itgaurav198
      New Member
      • Oct 2007
      • 41

      #3
      Originally posted by r035198x
      You pass the previosly selected value back to the JSP and use a simple if condition

      [CODE=java]<%
      if(value.equals ("previousValue ")) {
      %>
      <OPTION value ="<%=value%> " selected><%=val ue%> [/CODE]
      Hi,

      Thanks for ur reply.

      But in this case i will get duplicate items in same dropdown list i.e. One due to if condition and other due to static options.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by itgaurav198
        Hi,

        Thanks for ur reply.

        But in this case i will get duplicate items in same dropdown list i.e. One due to if condition and other due to static options.
        You should use a loop to populate the other "static" options. Put them in an array and populate them from the array.

        Comment

        • itgaurav198
          New Member
          • Oct 2007
          • 41

          #5
          Hi,

          Thanks for your valuable suggestion. I think this will get solved now.

          I want to ask one more thing.
          Is it possible that we use
          Code:
          HttpSession session=request.getSession(true)
          session.setAttribute("str",str);
          and
          Code:
          request.setAttribute("str1",str1);
          and then use RequestDispatch er and forward to pass the request in same servlet.

          Thnax

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by itgaurav198
            Hi,

            Thanks for your valuable suggestion. I think this will get solved now.

            I want to ask one more thing.
            Is it possible that we use
            Code:
            HttpSession session=request.getSession(true)
            session.setAttribute("str",str);
            and
            Code:
            request.setAttribute("str1",str1);
            and then use RequestDispatch er and forward to pass the request in same servlet.

            Thnax
            Should work but normally you want to use one and not both methods.
            Use the session to store attributes that belong to a session and use request.setAttr ibute when you want to pass a value from a servlet to JSP. You'd then use requestDispatch er with include(request , response)

            Comment

            • abcxyzgoo
              New Member
              • Jul 2015
              • 2

              #7
              session["value"]!=null
              {
              //bind ur code
              }

              Comment

              Working...