javapostgresqlservletserror-handlingjquery-form-validator

How to send error message from servlet to jsp page?


My jsp page consists of a form that takes in an email address. I then use java to check if my postgresql database already contains that email address. Right now my servlet just redirects the user back to the Registration page if the email address already exists.

What I am trying to do is write an error message on the form "The email address you entered already exists" with java. I haven't figured out how to do it.

Is there some way I could do this with jQuery form validation? I am using that right now for required fields.


Solution

  • Here you need to pass information from servlet to JSP.

    To Pass parameter from servlet to JSP or vice versa.

    Use Session to pass value from one page to another (here).

    In Servlet

    request.getSession().setAttribute("email", "exist/notexist");
    

    In JSP

    Make a blank div just beside the email textbox and set its content based on the value of String s1 which is set from session.getAttribute("key") method.

        <%! String s1 = ""; %>
        <% s1  = (String) session.getAttribute("email");%>
        <% if(s1.equals("exist")){ %>
        <div class="besideemailbox" style="color : red">Email Already exist</div>
        <% }else if(s1.equals("something")){ %>
        <div class="besideemailbox" style="color : green">ok or a tick</div>
        <% } %>