Collect and save submitted values of multiple dynamic HTML inputs back in servlet

Given this simplified model: public class Item { private Long id; private String foo; private String bar; // … } Here’s how you can do it provided ${items} is List<Item>: <c:forEach items=”${items}” var=”item”> <tr> <td> <input type=”hidden” name=”id” value=”${item.id}” /> <input name=”foo_${item.id}” value=”${fn:escapeXml(item.foo)}” /> </td> <td> <input name=”bar_${item.id}” value=”${fn:escapeXml(item.bar)}” /> </td> </tr> </c:forEach> (note the … Read more

Strip whitespace from jsp output

There is a trimWhiteSpaces directive that should accomplish this, In your JSP: <%@ page trimDirectiveWhitespaces=”true” %> Or in the jsp-config section your web.xml (Note that this works starting from servlet specification 2.5.): <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <trim-directive-whitespaces>true</trim-directive-whitespaces> </jsp-property-group> </jsp-config> Unfortunately if you have a required space it might also need strip that, so you may need … Read more

How do I call a specific Java method on a click/submit event of a specific button in JSP?

Just give the individual button elements a unique name. When pressed, the button’s name is available as a request parameter the usual way like as with input elements. You only need to make sure that the button inputs have type=”submit” as in <input type=”submit”> and <button type=”submit”> and not type=”button”, which only renders a “dead” … Read more

Passing an object from JSP page back to Servlet

Learn how HTTP works: Client (usually, a web browser) fires HTTP request. Server retrieves HTTP request. Servletcontainer creates new HttpServletRequest and HttpServletResponse objects. Servletcontainer invokes appropriate servlet with those objects. Servlet processes request and forwards request and response to JSP. JSP writes to the response body. Servletcontainer commits HTTP response and destroys request and response … Read more

How perform validation and display error message in same form in JSP?

Easiest would be to have placeholders for the validation error messages in your JSP. The JSP /WEB-INF/foo.jsp: <form action=”${pageContext.request.contextPath}/foo” method=”post”> <label for=”foo”>Foo</label> <input id=”foo” name=”foo” value=”${fn:escapeXml(param.foo)}”> <span class=”error”>${messages.foo}</span> <br /> <label for=”bar”>Bar</label> <input id=”bar” name=”bar” value=”${fn:escapeXml(param.bar)}”> <span class=”error”>${messages.bar}</span> <br /> … <input type=”submit”> <span class=”success”>${messages.success}</span> </form> In the servlet where you submit the form to, … Read more

How to use relative paths without including the context root name?

If your actual concern is the dynamicness of the webapp context (the “AppName” part), then just retrieve it dynamically by HttpServletRequest#getContextPath(). <head> <link rel=”stylesheet” href=”https://stackoverflow.com/questions/4764405/${pageContext.request.contextPath}/templates/style/main.css” /> <script src=”${pageContext.request.contextPath}/templates/js/main.js”></script> <script>var base = “${pageContext.request.contextPath}”;</script> </head> <body> <a href=”${pageContext.request.contextPath}/pages/foo.jsp”>link</a> </body> If you want to set a base path for all relative links so that you don’t need to … Read more

Hidden features of JSP/Servlet [closed]

Note: I find it hard to think of any “hidden features” for JSP/Servlet. In my opinion “best practices” is a better wording and I can think of any of them. It also really depends on your experience with JSP/Servlet. After years of developing you don’t see those “hidden features” anymore. At any way, I’ll list … Read more