Reputation: 287
I need a little help to tie a JSP and two Java Servlet together. I have got the following:
On the JSP:
- First two radio buttons with onchange="submit()"
- a div container, which will only appear if one of the radio buttons is active (containing the next two)
- some dropdowns, populated according to the choice of the radio button
- some input fields
Two Servlets:
- one for populating the dropdowns
- second for validating everything else and filling the db/redirecting to another page in case of success, ...
The population works well, so does the database part.
There are only some combinations allowed for the different drop down menus (there are in reality more values and more drop down menus as in the snippet below). My problem is: Whenever validation fails, the JSP reloads (that's obviously ok) and my choices made vanished and I start choosing between the radio button options.
To pass the value of the radio button from Servlet 1 to Servlet 2 seems to work perfectly. (I tried already to use the same methods (request.set...) within the reload of Servlet2, but it did not work.) Also the validation works fine.
Question: How can I achieve, that after validation the radio-buttons (and other input fields) are preselected as they were before? I just want to show an error message and keep all input values.
If this is important, here are some parts of the code, names changed, hopefully consistently. If someone knows how to put this in an on-demand/spoiler container, feel free to edit.
input.jsp
<body>
<span class="messages">${messages.ERROR}</span>
<form action="/project/chooseRadio" method="get">
<h2>Radio</h2>
<input type="radio" name="option" onchange="submit()"
value="option1"
${param.option == 'option1' ? 'checked' : ''}>
<input type="radio"
name="option" onchange="submit()" value="option2"
${param.option== 'option2' ? 'checked' : ''}>
</form>
<form action="/project/input" method="post">
<div
style="display:${(option == 'option1' || option == 'option2') ? 'block' : 'none'}">
<select name="dd1">
<c:forEach items="${dd1}" var="dd1">
<option><c:out value="${dd1}" /></option>
</c:forEach>
</select>
<select name="dd2">
<c:forEach items="${dd2}" var="dd2">
<option><c:out value="${dd2}" /></option>
</c:forEach>
<input type="text" name="inputfield1" required="true" /> <br>
<input type="text" name="inputfield2" required="true" /> <br>
<input type="submit" name="submit"
value="submit" />
</span>
</div>
</form>
</body>
chooseRadio-Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// option1
int[] dd1_1 = { 1, 2, 3, 4 };
int[] dd2_1 = { 1, 2, 3, 4 };
// option2
int[] dd1_2 = { 9, 8, 7, 6 };
int[] dd2_2 = { 9, 8, 7, 6 };
if (request.getParameter("option") != null) {
switch (request.getParameter("option")) {
case "option 1":
request.setAttribute("option", "option1");
request.setAttribute("dd1", dd1_1);
request.setAttribute("dd2", dd2_1);
break;
case "option2":
request.setAttribute("gratingType", "option2");
request.setAttribute("dd1", dd1_2);
request.setAttribute("dd2", dd2_2);
break;
default:
break;
}
}
request.getRequestDispatcher("/WEB-INF/input.jsp").forward(request, response);
}
input-Servlet
@WebServlet("/input")
public class InputServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/input.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DTO dto = new DTO();
Map<String, String> messages = new HashMap<String, String>();
switch (request.getParameter("option")) {
case "option1":
dto.setOption("option1");
break;
case "option2":
dto.setOption("option2");
break;
default:
break;
}
dto.setInputfield1(Integer.parseInt(request.getParameter("inputfield1")));
// ... dto.set for other fields ... //
// save dto in database
// validation
if(validCombination(...)){
request.getRequestDispatcher("/WEB-INF/output.jsp").forward(request, response);
}
else {
messages.put("ERROR", String.format("this is not a valid combination."));
request.setAttribute("messages", messages);
request.getRequestDispatcher("/WEB-INF/input.jsp").forward(request, response);
}
}
Upvotes: 0
Views: 3234
Reputation: 28101
You have three options really
Options 1&2 will leave the values in the fields. Option 3 will require you to populate the values as well as showing the error message.
Upvotes: 2