0

I submit JSP page with AJAX,

<script type="text/javascript">
    var form = $('#form1');
    form.submit(function () {

        $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            data: form.serialize(),
            success: function (data) {
                var x = data;    
                $('#result').attr("value", x);    
            }
        });

        return false;
    });
</script>

In my Servlet,

request.setAttribut("test","asd");

is used.

Now I want to use request.getAttribute("test"); in my JSP page but I can't find.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Chintan
  • 545
  • 1
  • 9
  • 24

1 Answers1

0

You are sending the serialized content of the form here:

data: form.serialize()

Make sure that inside your form you have a <input> field with name="test". Then inside your servlet you could retrieve the value using the getParameter method:

public class MyServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        String test = request.getParameter("test");
        ....
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • in servlet i am also write request.setAttribute("test1","asd"); and i need that request value in jsp but i can't get request attribut in jsp which servlet set – Chintan Mar 29 '14 at 09:42
  • because my requirement is send HaseMap object from servlet to jsp when page is submit using ajax – Chintan Mar 29 '14 at 09:44
  • Since this is an AJAX call to the servlet, the page will not reload. So any changes you might be doing in your servlet will not reflect to the UI unless you manually reflect them in the `success` callback. Ideally when working with AJAX your server side action should either return only some partial HTML that you could replace in the DOM or JSON. – Darin Dimitrov Mar 29 '14 at 09:44
  • ok so it means i can't get map object from servlet in jsp file – Chintan Mar 29 '14 at 09:46
  • The `data` parameter that is passed to the `success` function will contain the result of the servlet execution. If you are rendering some JSP then it will contain the markup of this JSP. You should modify your servlet so that it returns JSON result. Then you will be able to assign it to the value of the corresponding input field. Here's an example: http://stackoverflow.com/a/2012031/29407 – Darin Dimitrov Mar 29 '14 at 09:50