4

I have a Dropdownlist

<select name="cate">
  <option value="h">India</option>
  <option value="m">USA</option>
  <option value="c">England</option>
</select>
<input type="submit" value="show">

While clicking on show button,in the below table it will show the selected country details. Details are coming from database,and each click the page will refresh.

My problem is that, if a user selects England on Dropdown list and clicks on show button, it will show the details, but the dropdown list will show the first name only. I want to show the selected value name.

Please help me.

Raibaz
  • 9,280
  • 10
  • 44
  • 65
Prashobh
  • 9,216
  • 15
  • 61
  • 91

4 Answers4

3

HTML CODE

<input type=hidden id ="selection" name="selection" value="">
<select name="cate">
  <option onclick="document.getElementById('selection').value='h';" value="h">India</option>
  <option onclick="document.getElementById('selection').value='m';" value="m">USA</option>
  <option onclick="document.getElementById('selection').value='c';" value="c">England</option>
</select>

JSP CODE

<%
  String selected = request.getParameter("selection");
%>

Now when you are on the JSP page you have an indication of what the user selected. When you render the element append the select attribute to the selected option

eg: < option value="h" selected > India < / option >

UPDATE In the place where you display the dropdown you have to do some kind of validation where you need to check the selection value and what you are actually printing.

if(selected ==null || selected.equals('')){
    //do regular printing of the dropdown as you do now
}else{
  out.print("<select>");
  while(rs.next()){
    if(rs.getString("VALUE").equals(selected )){
        out.print("<option value=\""+rs.getString("VALUE")+"\" SELECTED >"+rs.getString("COUNTRY")+"</option>");
    }else{
        out.print("<option value=\""+rs.getString("VALUE")+"\" >"+rs.getString("COUNTRY")+"</option>");
    }
  }
  out.print("</select>");
}

PS: The code is written on the fly and could need some exception handling or some typos may exist

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • thank u,bt it is not working.Clicking on my show button,it will go to servlet,In servlet i written select query,After submission the page will refresh ,then option value will be default one. – Prashobh Jul 03 '12 at 11:38
  • 1
    The value for the ` – Anthony Grist Jul 03 '12 at 11:43
  • @Anthony Grist This was to indicate an obvious way to get the selected option so that OP could embed it into his code. You are right that it is something unnessesary – MaVRoSCy Jul 03 '12 at 11:46
3
<input type="hidden" name="selectedValue" value="0"/>  

put the above one in just below of your select tag

and do this in your servlet

String selectedValue = `request.getParameter("selectedValue")` 

now set the selectedValue into the servlet request

create a JS function in your final jsp

function selectedValue(){  

    var value =<%=request.getParameter("selectedValue")%>;  
    if(value !=null)  
        {

        document.f1.slvalue.selectedIndex=value ;          

        }    

} 

call the selectedValue() function on bodyload of your final jsp page. "slvalue" is a name of your select tag

subodh
  • 6,136
  • 12
  • 51
  • 73
0

You can add selected to an <option> tag so that it's the initially selected value for the page.

<option value="h" selected>India</option>

would select the India option when the page loads.

You'll need to add some logic to your JSP to add selected to the correct <option> tag depending on the value that was selected when the page was submitted.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
0

For a clean method you can set a cookie.

look at this jquery method: jQuery cookies setting select drop down value after page refresh

Community
  • 1
  • 1
RAN
  • 1,443
  • 3
  • 19
  • 30