Out of the many solutions I've found, I can't seem to get any of them to work for me. I've got a dropdown list in my jsp file:
<select name="chosenOne" onchange="javascript:getUsers(this.value);">
<option value="0" onclick="javascript:getUsers(this.value);">All Modules</option>
<c:forEach items="${modules}" var="module">
<option value="${module.id}"><c:out value="${module.title}"/></option>
</c:forEach>
</select></p>
It populates dynamically from my database, except for the "All Modules" option. Here's my javascript function for the onchange event:
<script type="text/javascript">
function getUsers(id) {
if (id != "0"){
document.updateForm.id.value = id;
}
else{
document.updateForm.id.value = "0";
}
document.updateForm.submit();
}</script>
And here's my servlet code that deals with the dropdown box amongst other things:
protected void process(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
long modID = 0;
String url = "jsp/user/administration.jsp";
request.setAttribute("modules", dataAccessor.getAllModules());
if (isParameterValid(request.getParameter("id"))) {
modID = Long.parseLong(request.getParameter("id"));
request.setAttribute("users", getUsersFromModule(modID));
System.out.println(modID);
} else if (!isParameterValid(request.getParameter("id"))) {
request.setAttribute("users", dataAccessor.getAllUsers());
} else {
request.setAttribute("errorMessage", "There was a problem retrieving users!");
url = "jsp/error.jsp";
}
//request.setAttribute("formerSelect", modID);
request.getRequestDispatcher(url).forward(request, response);
}
So how can I get the selected dropdown value to remain in the dropdown box after the form refreshes? I've fiddled around with setting an attribute "formerSelect" which just contains the value of the previously selected item in the dropdown. But then for some reason it rendered my dropdown useless when I tried to assign it to the "selected" value within my options tag. Any help is much appreciated.