users@jersey.java.net

[Jersey] Challenge: Include JSON output for JSP

From: Kevin Zhang <zhangyongjiang_at_yahoo.com>
Date: Sun, 21 Sep 2014 09:11:55 -0700

Hi All,

Suppose I have implemented two web service /ws/author/${authorId} (return a User object) and /ws/books-of-books/${authorId} (return a list of books). I also have a jsp file ..../author.jsp?authorId=xxxxx.

In my jsp file, I would like to do sth like

<scripttype="text/javascript">
    var author = <% request.getRequestDispatcher("/ws/author/" + request.getParameter("authorId")).include(request, response);%>
    var books = <% request.getRequestDispatcher("/ws/books-of-author/" + request.getParameter("authorId")).include(request, response);%>

    // some jquery scripts which use author and books variables.
</script>

In my test code, if I run curl "http://127.0.0.1:8080/json-processing-webapp/ws/document", I got the json output correctly.

my test index.jsp is

    <scripttype="text/javascript">
        docs = <% request.getRequestDispatcher("/ws/document").include(request, response);%>
        document.write(docs.length);
    </script>

The output of my index.jsp is below. The json is not printed as expected.

    <script type="text/javascript">
        docs =
        document.write(docs.length);
    </script>

The reason why I include web service in JSP is because I would like to get multiple jsons and from web service.

Thanks a lot,
Kevin