Creating a json object in jsp and using it with JQuery

Here’s an example you may take a look at. Basically your JSP page might look like this:

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%
    JSONObject json = new JSONObject();
    json.put("title", "TITLE_TEST");
    json.put("link", "LINK_TEST");
    out.print(json);
    out.flush();
%>

and on the client:

$.ajax({
    url : 'search.jsp',
    data : { search: 'test' },
    dataType: 'json',
    success : function(json) {
        alert(json.title);
    }
});

And here are even more examples.

Leave a Comment