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.

vivekpansara
  • 895
  • 1
  • 6
  • 14
dimilalabar
  • 85
  • 4
  • 12
  • Is `System.out.println(request.getAttribute("message"));` being display in console? –  Jun 12 '15 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 '15 at 11:52
  • yes its displayed in the console – dimilalabar Jun 12 '15 at 12:00
  • i changed /index.jsp with index.jsp - nothing happend – dimilalabar Jun 12 '15 at 12:02
  • post your full code , like how you are calling servlet ? – Keval Jun 12 '15 at 12:04
  • This seems to be your answer http://stackoverflow.com/questions/12351107/request-attributes-not-available-in-jsp-page-when-using-sendredirect-from-a-serv – slarge Jun 12 '15 at 12:14

2 Answers2

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.

gprathour
  • 14,813
  • 5
  • 66
  • 90
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());
        }
Keval
  • 1,857
  • 16
  • 26