Search code examples
jspservletsjsp-tags

How can I stop a jsp tag from showing default null message?


This is the jsp page registration_of_an_user.jsp

<body>

<div id="container">

    <form action="User_Controller_Servlet" method="POST">

        <input type="hidden" name="command" value="REGISTRATION_OF_AN_USER" />

        <table>

            <tbody>

                <tr>
                    <td><label>User name</label></td>
                    <td><input type="text" name="u_name"></td>
                </tr>

                <tr>
                    <td><label>User email</label></td>
                    <td><input type="text" name="u_email"></td>
                </tr>

                <tr>
                    <td><label></label></td>
                    <td><input type="submit" value="Register"></td>
                </tr>

            </tbody>

        </table>

        <p><%=request.getAttribute("message")%></p>

    </form>

</div>

This is the controller portion.

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

    try {
        String _command = request.getParameter("command");

        switch (_command) {

        case "REGISTRATION_OF_AN_USER":
            registration_of_an_user_controller_servlet(request, response);
            break;

        default:

        }
    } catch (Exception e) {
        throw new ServletException(e);
    }

}
private void registration_of_an_user_controller_servlet(HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    String _user_name = request.getParameter("u_name");
    String _user_email = request.getParameter("u_email");

    User user = new User(_user_name, _user_email);

    if (user_dao.email_exist_or_not_dao(_user_email)) {
        request.setAttribute("message", "An user with this email already exists.");
        request.getRequestDispatcher("/registration_of_an_user.jsp").forward(request, response);
    }

    user_dao.registration_of_an_user_dao(user);

    get_list_of_users_controller_servlet(request, response);

}

Whenever the registration_of_an_user.jsp is running, its showing null message at the place of that paragraph tag. It would be great if I could avoid this message from showing default value. How can I do that? Can I do it from controller portion or should I use java script for that? I would really appreciate your suggestion on this.


Solution

  • Just put your <p>..</p> inside condition.

    <div id="container">
    
        <form action="User_Controller_Servlet" method="POST">
    
            <input type="hidden" name="command" value="REGISTRATION_OF_AN_USER" />
    
            <table>
    
                <tbody>
    
                    <tr>
                        <td><label>User name</label></td>
                        <td><input type="text" name="u_name"></td>
                    </tr>
    
                    <tr>
                        <td><label>User email</label></td>
                        <td><input type="text" name="u_email"></td>
                    </tr>
    
                    <tr>
                        <td><label></label></td>
                        <td><input type="submit" value="Register"></td>
                    </tr>
    
                </tbody>
    
            </table>
             <%if(request.getAttribute("message")!=null){ %>
    
                <p><%=request.getAttribute("message")%></p>
             <% } %>
    
        </form>
    

    Give reply if it work