0

I want that after submitting my form and then for some reason again forwarding to the same page, the selected option in the drop-down list should retain the selected value I entered first time.

<form action="CitySelection" method="POST">

     <select name="cityname" id="myselect" onchange="this.form.submit()">
         <option value="england">england</option>
         <option value="france">france</option>
         <option value="spain">spain</option>
     </select>

</form>

How I can enhance my above code of form with this feature, any suggestions?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ramesh
  • 1,252
  • 3
  • 12
  • 30
  • What do you mean by the word "retain"? Can you please explain clearly that what are you trying to do and what are the problems you are facing? – Sazzadur Rahaman Nov 27 '12 at 15:02
  • for example if user selects 'spain' in the dropdown list then form will be submitted then action will be performed then some results will be displayed on jsp file, now if we see drop down there 'england' will be selected but i want 'spain' to be selected because user selected 'spain'. – Ramesh Nov 28 '12 at 10:07

1 Answers1

0

Why you are putting hard coded city list in your select menu? To do that your jsp page should look like bellow using c taglib:

<%@ taglib prefix="c" uri="java.sun.com/jsp/jstl/core" %>

<form action="<SUBMITTING_URL>" method="post">
      <select name="cityname" id="myselect" onchange="this.form.submit()">
         <c:foreach var="cityname" items="${cityList}">
            <c:choose>
               <c:when test="${not empty selectedCity && selectedCity eq cityname}">
                   <option value="${cityname}" selected = "true">${cityname}</option>
               </c:when>
               <c:otherwise>
                   <option value="${cityname}">${cityname}</option>
               </c:otherwise>
            </c:choose>
         </c:foreach>
      </select>
</form>

In your servlet mapped with your <SUBMITTING_URL> should have a doPost method like bellow:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   String cityName = request.getParameter("cityname"); //retrieving value from post request.
   //do your necessary coding.
   if(validation error or as your wish){
      List<String> cityList = new ArrayList<String>();
      cityList.add("england");
      cityList.add("france");
      cityList.add("spain");
      request.setAttribute("cityList",cityList);
      request.setAttribute("selectedCity",cityName);

      RequestDispatcher dispatcher = request.getRequestDispatcher("cityform.jsp");  
      dispatcher.forward(request, response);
   } else {
      //do other things
   }
}

And your doGet method should look like bellow:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      List<String> cityList = new ArrayList<String>();
      cityList.add("england");
      cityList.add("france");
      cityList.add("spain");
      request.setAttribute("cityList",cityList);
      request.setAttribute("selectedCity",null);

      RequestDispatcher dispatcher = request.getRequestDispatcher("cityform.jsp");  
      dispatcher.forward(request, response);
}

Now when you like to go to the page second time, you will see the selected value, you have chosen.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52