0

I am doing some server side form validation and in the case that one or more of the fields is incorrectly filled out, an array gets populated with all of the error messages. On the client side, I have a scriplet that checks for the existence of any error messages and if there are any, it displays them. When the page comes from the servlet it knows if it has failed or not because on a successful submission, it would not reload the form jsp page at all.

This is how I am displaying the error:

<%if(request.getSession().getAttribute("errors") != null){ %>
<jsp:include page="error.jsp"></jsp:include>
<br>
<% } %>

And the error.jsp page is:

<%@ page import="java.util.ArrayList" %>

<h3>Oops...We Have a Problem</h3>
Please review and fix the following errors.
<br>
<%
ArrayList errMessages = (ArrayList)request.getSession().getAttribute("errors");
for(int i=0; i<errMessages.size(); i++){
out.println(errMessages.get(i));
%>
<br>

This all works fine, but I am following the MVC/Model 2 Paradigm approach in where I keep the code confined to servlets and the html (display objects) confined to jsp pages. Obviously, this small example breaks the rules.

Is there a way to "pre-build" the jsp page on the servlet so it knows to display the error.jsp and I can do the whole array abstraction on the server? In this example it only seems like a tiny bit of code in the jsp that can't hurt, but in other examples I can see this code becoming a much larger section of the page and that is what I would like to avoid.

ryandlf
  • 27,155
  • 37
  • 106
  • 162
  • Just a sincerely curious question: you're not the first one to ask such kind of question. But I really wonder how you (and others) can be aware of the MVC approach and of its rules, without being aware of the JSP EL, the JSTL and taglibs. Those two go hand in hand. Where did you learn about servlet/JSP development? – JB Nizet Oct 17 '11 at 12:45
  • I have been on the graphics/display side of programming for the majority of my career but over the past year have been reading books and teaching myself more in depth java and servlets/jsp interested me. I work for a small company so fortunately I am able to take on a small side project in which I can teach myself while I work. I apologize that I do not have the proper education that you have, but everyone has to start somewhere. Would JSTL and taglibs solve the problem I am asking about? In the future, could you please respond in the form of a constructive answer? – ryandlf Oct 17 '11 at 12:51
  • My point was not to be unconstructive or condescending. My answer would have been identical to the one of BalusC (so I upvoted him). It was just a curiosity: most of the good books or tutorials talking about JSPs these days should talk about the JSP EL, the JSTL, and custom JSP tags. Especially if they talk about MVC. But the fact is that many JSP questioners here still use scriptlets and haven't heard about those topics, which seems very strange to me. So I was just wandering where all these questioners learnt about JSP technology. Is it on a bad web site, in an old book? – JB Nizet Oct 17 '11 at 15:32

1 Answers1

1

Just use taglibs instead of scriptlets to control the flow in JSP. JSTL is a standard JSP taglib and it offers flow control tags.

<c:if test="${not empty errors}">
    <jsp:include page="error.jsp" />
</c:if>

and

<c:forEach items="${errors}" var="error">
    <c:out value="${error}" /><br/>
</c:forEach>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555