Tom Joad
Tom Joad

Reputation: 585

Can't get error messages (validation) from Servlet to JSP

I have a problem with Servlets/JSP. I am trying to validate JSP's HTML field in Java Servlet class and bring back that (possible) error message to JSP from Servlet. I can't solve this out even though I am following tutorials.

index.jsp:

   <form action="addWine" method="post">
        <p>
            <input type="text" name="name"/>          
            <input type="submit" value="submit">
            <span class="error">${messages.error}</span>
        </p>
    </form>


AddingWines.java

public class AddingWines extends HttpServlet {

...

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


   Map<String, String> messages = new HashMap<String, String>();
   request.setAttribute("messages", messages);

    String name = request.getParameter("name");
    if(name.trim().isEmpty()){
        messages.put("name", "give a name");
    }

    //No errors in HTML field

    if (messages.isEmpty()) {
        messages.put("noErrors", String.format("Welcome blabla"));
    }

    request.getRequestDispatcher("greatSucceess.jsp").forward(request, response);


web.xml

    <servlet>
    <servlet-name>AddingWines</servlet-name>
    <servlet-class>Servlets.AddingWines</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>AddingWines</servlet-name>
    <url-pattern>/addWine</url-pattern>
</servlet-mapping>

NetBeans isn't throwing any errors. It just directs to greatSucceess.jsp even when the HTML field is empty. What to do? (Btw, is there enough info?)

Upvotes: 1

Views: 3301

Answers (4)

user2810910
user2810910

Reputation: 287

Previous poster mentioned that:
request.setAttribute("messages", messages); should be added after you set your messages, and not before because your are always passing an empty Hashmap.
I believe is may not be correct. Even though messages is added via setAttribute before having data added to messages, its only upon calling dispatch is the reference to messages may be unusable. The reference to messages in your program that you are adding data to is still valid even after a call to setAtribute. In any case, I agree with the previous poster that data should be added to messages first, then call setAttribute in order to make the code easier to read.

Upvotes: 0

MaVRoSCy
MaVRoSCy

Reputation: 17839

In the case that you check the messages parameter in greatSucceess.jsp then the line

request.setAttribute("messages", messages);

should be added after you set your messages, and not before because your are always passing an empty Hashmap.

In the case that you want to forward the request to another jsp if errors exist then you should add a validation condition and not always redirect the response to the same page.

I would do something like this for example:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Map<String, String> messages = new HashMap<String, String>();
    String name = request.getParameter("name");
    if (name.trim().isEmpty()) {
        messages.put("name", "give a name");
    }

    //handle messages in each jsp separately
    if (messages.isEmpty()) {
        messages.put("noErrors", String.format("Welcome blabla"));
        request.setAttribute("messages", messages);
        request.getRequestDispatcher("greatSucceess.jsp").forward(request, response);
    }else{
        request.setAttribute("messages", messages);
        request.getRequestDispatcher("form.jsp").forward(request, response); 
    } 
}

Upvotes: 0

user2810910
user2810910

Reputation: 287

I suspect this is the problem: You have this line: ${messages.error} However, messages is a Map object. Map object doesn't have a function called getError(), therefore message.error isn't correct. You need to access the data in messages another way.

Upvotes: 0

Georg Leber
Georg Leber

Reputation: 3580

In your servlet you redirecting every request to greatSuccess.jsp, see your last line:

request.getRequestDispatcher("greatSucceess.jsp").forward(request, response);

You should change the code as follows:

if (messages.isEmpty()) {
    request.getRequestDispatcher("greatSucceess.jsp").forward(request, response);
} else {
    // show page with form
}

Upvotes: 1

Related Questions