0

i have a servlet my goal is to return a customer object from the process request, where i can then access this object in my jquery. Does anyone know how i can go about doing this?

 e.g. myObject.getMethod()

Servlet Code:

 Customer loginResult;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            //request.setAttribute("customerFirstName", loginResult.getFirstName()); //String Value
            //request.setAttribute("customerID", loginResult.getCustomerID()); //IntegerValue
            out.println(loginResult);

        } finally {
            out.close();
        }
    }

JSP CODE:

<script type="text/javascript">
$().ready(function() {
    $('#submit').click(function() {

        var dataf = 'email=' + $('#email').val()
                + '&password=' + $('#password').val();
        $.ajax({
            url: "http://localhost:8080/RetailerGui/loginServlet",
            type: "get",
            data: dataf,
            success: function(data) {
            alert(data);

            }
        });
        return false;
    });
});
</script>

Can someone please assist me in resolving this issue, thank you for your help in advance.

KSM
  • 262
  • 2
  • 6
  • 16
  • 1
    This is not JSP code, this is just javascript... – Virus721 Mar 19 '14 at 14:45
  • @Virus721 sorry removed the tag my code is in jsp file. Therefore i accidentally selected it. – KSM Mar 19 '14 at 14:46
  • This may help http://stackoverflow.com/questions/3832792/access-request-object-in-javascript – Susie Mar 19 '14 at 14:55
  • @Susie that's useful when you will forward to a page, but OP's handling an ajax request from the servlet, so nor EL nor JSTL will work after server response. – Luiggi Mendoza Mar 19 '14 at 14:57

1 Answers1

0

Since you want to handle an ajax request using a Servlet, the best bet you have is writing the data of your custom object into the response. The easier way I found to accomplish this is using JSON. There are lot of libraries that handles JSON conversion from objects to Strings and vice versa, I recommend using Jackson. This is how your code should look like.

Servlet code:

import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/loginServlet") //assuming you're using Servlet 3.0
public class YourServlet extends HttpServlet {

    //Jackson class that handles JSON marshalling
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    //login operation should be handled in POST
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        Customer loginResult = ...; //process data and get the loginResult instance
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        //marshalling the data of your loginResult in JSON format
        String json = OBJECT_MAPPER.writeValueAsString(loginResult);
        response.getWriter().write(json);
    }
}

Javascript code:

<script type="text/javascript">
$().ready(function() {
    $('#submit').click(function() {
        var dataf = 'email=' + $('#email').val()
                + '&password=' + $('#password').val();
        $.ajax({
            url: "http://localhost:8080/RetailerGui/loginServlet",
            type: "post", //login action MUST be post, NEVER a get
            data: dataf,
            success: function(data) {
                //shows the relevant data of your login result object in json format
                alert(data);
                //parsing your data into a JavaScript variable
                var loginResult = JSON && JSON.parse(data) || $.parseJSON(data);
                //now you can use the attributes of your loginResult easily in JavaScript
                //for example, assuming you have a name attribute in your Customer class
                alert(loginResult.name);
            }
        });
        return false;
    });
});
</script>

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332