How to retain the dropdown value after submitting the form

I had this question after viewing Retain the value of Drop down box in jsp.
I tried the below code but i'm getting error : alloc_Type is undefined on refresh.
 String riskTypeValue = request.getParameter("riskType");
if(riskTypeValue == null)riskTypeValue = "0";

String allocTypeValue = request.getParameter("allocType");
if(allocTypeValue == null)allocTypeValue = "0";

function retainValue(){
      //alert("retainValue "+alloc_Type +" && " +risk_Type);
    var element1 = document.forms[0].allocType;
    for(x = 0; x < element1.length; x++){
            if(element1.options[x].value == "<%=alloc_Type%>")
                  element1.options[x].selected = true;
    var element2 = document.forms[0].riskType;
    for(x = 0; x < element2.length; x++){
            if(element2.options[x].value == "<%=risk_Type%>"){
                  element2.options[x].selected = true;

                        <td class="smalltext" width="150px"><b>Allocation Type:</b></td>
                        <td class="smalltext">
                               <select id="allocType"
                                          class="filter_smalltext" name="allocType">
                                    <c:forEach items="${dataBO.alloc_types}" var="allocTypes">
                                          <option value="${allocTypes.key}" ${ allocTypes.key eq dataBO.alloc_type ? 'selected':'' }>
                                                <c:out value="${allocTypes.value}"></c:out>
Sneha RajuAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Olaf DoschkeSoftware DeveloperCommented:
if(element1.options[x].value == "<%=alloc_Type%>")

Every other line of code is talking of allocType and allocTypes without underscore, why should <%=alloc_Type%> work, if no previousline defines something like that?

In one of the first lines you define: String allocTypeValue = request.getParameter("allocType") so I assume you would need to use that name: allocTypeValue or <%=allocTypeValue%>, as allocTypeValue is defined.

Bye, Olaf.
Did you change from your original question?  
I wrote a demonstration code.  If you are using JSTL, then I can rewrite it.
    String cr = "";
	String mr = "";
	String both = "";
	String none = "";
	String selectedString = "selected=\"selected\"";
	String selectedValue = request.getParameter("riskType");
	if(selectedValue == null)none = selectedString;
	if("CR".equals(selectedValue))cr = selectedString;
	if("MR".equals(selectedValue))mr = selectedString;
	if("BOTH".equals(selectedValue))both = selectedString;
		<select name="riskType">
            <option value="" <%=none%>>Choose One</option>
            <option value="CR" <%=cr%>>CR</option>
            <option value="MR" <%=mr%>>MR</option>
            <option value="BOTH" <%=both%>>BOTH</option>
		<input type='submit' value='Submit'/>
	Selected value is <%=selectedValue%>

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I believe I answered the question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.