4

Servlet Code:

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

    String action = request.getParameter("action");

    Connection conn = null;

    try {
        conn = ds.getConnection();
    } catch (SQLException e) {
        throw new ServletException();
    }

    UserOperations uo = new UserOperations(conn);

    if (action.equals("mainbutton")) {
        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String address = request.getParameter("address");
        String country = request.getParameter("country");
        String state = request.getParameter("state");
        String postalCode = request.getParameter("postalcode");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        User user = new User(name, surname, address, country, state,
                postalCode, phone, email);
        try {
            if (user.validate()) {
                uo.create(name, surname, address, country, state,
                        postalCode, phone, email);
            } else {
                request.setAttribute("message", user.getMessage());
                System.out.println(request.getAttribute("message"));
                request.getRequestDispatcher("index.jsp").forward(request,
                        response);
            }

        } catch (SQLException e) {
            System.out.println("No connection to the database.");
            e.printStackTrace();
        }
    }

    try {
        conn.close();
    } catch (SQLException e) {
        throw new ServletException();
    }
}

JSP code:

<script type="text/javascript">
    $(document).ready(function(){
        $("#mainbutton").click(function(){
            var name=$('input[name=name]').val();
            var surname=$('input[name=surname]').val();
            var address=$('input[name=address]').val();
            var country=$('#country :selected').val();
            var state=$('#state :selected').text();
            var postalcode=$('input[name=postalcode]').val();
            var phone=$('#mobile-number').val();
            var email=$('input[name=email]').val();
            $.post("userctrl",
                    {
                      action: "mainbutton",
                      name: name,
                      surname: surname,
                      address: address,
                      country: country,
                      state: state,
                      postalcode: postalcode,
                      phone: phone,
                      email: email
                    });
        });
    });
</script>
<li>
    <p class="valid-error">
        <%= request.getAttribute("message") %>
    </p>
</li>
<li>
    <input id="mainbutton" class="mainbutton" type="button" value="Rush"/>
</li>

On click of rush button I fill the request with atribute "message". After redirect to the same page I see only null. How to get my request atribute? The sysout is printed to the console. Also tried to redirect to index.jsp instead of /index.jsp without success.

6
  • Is System.out.println(request.getAttribute("message")); being display in console?
    – user4768611
    Jun 12, 2015 at 11:51
  • are you sure request.getRequestDispatcher("/index.jsp").forward(request, response); is right ? bcz /index.jsp redirect to project index.jsp, it should be request.getRequestDispatcher("index.jsp").forward(request, response);
    – Keval
    Jun 12, 2015 at 11:52
  • yes its displayed in the console Jun 12, 2015 at 12:00
  • i changed /index.jsp with index.jsp - nothing happend Jun 12, 2015 at 12:02
  • post your full code , like how you are calling servlet ?
    – Keval
    Jun 12, 2015 at 12:04

2 Answers 2

2

Why this isn't working

You are submitting your form by AJAX and as we know AJAX is meant so that whole page doesn't get refreshed. Only a specific part of the page is refreshed.

Now your page creates an Ajax request which goes to servlet and servlet forwards that request again to JSP page. But note your page is already being displayed with earlier request. It is not getting reopened / refreshed by the request forwareded by Servlet.

That's why it doesn't print value.

Easiest way to get it done

Try to submit form without Ajax by action attribute in form.

2

Try This :

in your jsp

        $.post("userctrl",
                {
                  action: "mainbutton",
                  name: name,
                  surname: surname,
                  address: address,
                  country: country,
                  state: state,
                  postalcode: postalcode,
                  phone: phone,
                  email: email
                },
           success: function(response) {
                $(".valid-error").html(response)
           });

And from Servlet pass your messenge like this

        if (user.validate()) {
            uo.create(name, surname, address, country, state,
                    postalCode, phone, email);
        } else {
          PrintWriter out = response.getWriter();
          out.print(user.getMessage());
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.