Retaining the value of a select during and after a Struts action

How does one "hold on" to a selected value on a web page or JSP after the page reloads on completion of a submitted Struts action?

Who is Participating?
rrzConnect With a Mentor Commented:
I don't use Struts. So, I can't tell you about how this is done the Struts way.
But I can show you how JSP and Javascript can work together to accomplish what you want to do. Please copy and paste this demonstration JSP.  It is just a simple JSP that sends its request to itself.  
   String selectedValue = request.getParameter("selectNumber");
   if(selectedValue == null)selectedValue = "Please select a number";
         function retainValue(){
              var element = document.forms[0].selectNumber;
              for(x = 0; x < element.length; x++){
                      if(element.options[x].value == "<%=selectedValue%>"){
                            element.options[x].selected = true;
<body onload="retainValue();">
<select name="selectNumber" >
            <option value="Please select a number">Please select a number</option>
           <option value="one">one</option>
           <option value="two">two</option>
           <option value="three">three</option> 
           <option value="four">four</option> 
<input type="submit"/>

Open in new window

Jerry MillerConnect With a Mentor Commented:
I sometimes use a hidden field to store values to be used later in a process.

Another option I use is to enable view state.
jwright9Author Commented:
Hi jmiller, I am working in Java and am experiencing this problem in a JSP.  The hidden variable technique I tried.  It should have worked don't know what the problem is.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

I saw this, 
So I guess in Struts you populate your form bean and then get the value from it instead of using a scriptlet as I have posted.
I suggest that you ask a question about this topic at   
You might be able to get a Struts expert to help you.
jwright9Author Commented:
I really liked rrz's code example.  He or she really put effort into answering this for me.  Thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.