I have a jsp
file with a form of radiobuttons. With POST
I read this form once submitted.
However, after submitting the form the answers given in the form dissapear and I would like to have them displaying.
How to do it? I've been walking round the internet and I can't seem to find a resolution.
I have been trying with req.setAttribute("q" + i, "yes");
in a loop since the values taken from this form I keep in an array.
I have also thought of JS but have no experience with it.
No success. Could you please help?I just need a hint or something to start with.
EDIT:
In my JSP (keywords.jsp) file, there is a search form:
<form method="POST" action="keywords">
<p>
Are you looking for an urgent email?
<label><input type="radio" name="q0" value="yes" class="q0">Yes</label>
<label><input type="radio" name="q0" value="no" class="q0">No</label><br/>
</p>
<p>
Are you looking for a business email?
<label><input type="radio" name="q1" value="yes" class="q1">Yes</label>
<label><input type="radio" name="q1" value="no" class="q1">No</label><br/>
</p>
input class="btn btn-warning" type="submit" value="Search Keywords">
So when I click Submit - in doPost
I get values for these radiobuttons. t page reloads and the form shows again along with the search results. After reloading, in the form, no answer is checked.
What I want is that I want to have answers in the form checked after the reload of the form. Answers that were given before reload and should be displayed after reload are in ArrayList
.
I my Servlet I have:
for(int i = 0; i < keywords.getAnswersIDs().size(); i++) {
if("1".equalsIgnoreCase(keywords.getAnswersIDs().get(i))) {
req.setAttribute("q" + i, "checked");
LOGGER.info(MARKER, "For element " + i + " is yes.");
} else {
req.setAttribute("q" + i, "no");
LOGGER.info(MARKER, "For element " + i + " is no.");
}
}
at the end I also have this:
RequestDispatcher dispatcher = req.getRequestDispatcher("/keywords.jsp");
LOGGER.info(MARKER, "Dispatcher to keywords.jsp");
try {
dispatcher.forward(req, response);
} catch (ServletException e) {
LOGGER.debug(MARKER, "Caught ServletException " + e);
e.printStackTrace();
} catch (IOException e) {
LOGGER.debug(MARKER, "Caught IOException " + e);
e.printStackTrace();
}
I hope this helps.