String selected = req.getParameter("name") returns null. I need the value from the selected radio button. How should I get it? Thanks!
This is the form int he HTML file:
<form th:method="POST" th:action="@{/select}">
<ul th:each="o: ${orders}" style="list-style-type: none">
<input th:text="${o.getDescription()}" th:value="${o.getName()}" type="radio" name="color"><br>
</ul>
<br/>
<input value="Submit" type="submit">
</form>
And this is my servlet:
@WebServlet(urlPatterns = "/select")
public class SelectServlet extends HttpServlet {
private final SpringTemplateEngine springTemplateEngine;
public SelectServlet(SpringTemplateEngine springTemplateEngine) {
this.springTemplateEngine = springTemplateEngine;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
WebContext context = new WebContext(req, resp, getServletContext());
String selected = req.getParameter("color");
context.setVariable("selectedOption", selected);
this.springTemplateEngine.process("selectSize.html", context, resp.getWriter());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/select");
}
}
I have read many answers to other similar questions, but none of them solved my problem.
CodePudding user response:
You should use doGet() when you want to intercept HTTP GET requests. You should use doPost() when you want to intercept HTTP POST requests. Here in this given problem the error is that you are sending a post request but changing or implementing your code in doGet method try to write same code in doPost method.
CodePudding user response:
you are sending a post request from the form (method=POST
), but in servlet you have written code inside the doGet()
, so your code will not be executed by servlet as the servlet will execute the doPost()
method, so you should write your code inside the doPost()
. I hope this will resolve your problem.