title: How to return a JSON result with JSP in Struts 2
body: <p>I know that in Struts2 can be used json plugin to return a <code>json</code> type result. A <code>json</code> could also be returned from the <code>stream</code> result like in <a href="https://stackoverflow.com/a/17096564/573032">this</a> answer.</p>
  <p>On the Struts2 docs page for <a href="https://struts.apache.org/core-developers/ajax.html#ajax-results-with-jsp" rel="nofollow noreferrer">Ajax result with JSP</a>, I've found that it's possible to return <code>dispatcher</code> type result with JSP that outputs a JSON.</p>
  <blockquote>
  <pre><code>&lt;%@ page import=&quot;java.util.Iterator,
           java.util.List,
           com.esolaria.dojoex.Book,
           com.esolaria.dojoex.BookManager&quot; %&gt;
  &lt;%
      String bookIdStr = request.getParameter(&quot;bookId&quot;);
      int bookId = (bookIdStr == null || &quot;&quot;.equals(bookIdStr.trim())) 
          ? 0 : Integer.parseInt(bookIdStr);
      Book book = BookManager.getBook(bookId);
      if (book != null) {
          out.println(book.toJSONString());
          System.out.println(&quot;itis: &quot; + book.toJSONString());
      }
  %&gt;
  </code></pre>
  </blockquote>
  <p>But it's using scriptlets to write JSON to the out. I know that using scriplets in JSP is highly discouraged. But I couldn't find the answer for my problem in this question <a href="https://stackoverflow.com/q/3177733/573032">How can I avoid Java code in JSP files, using JSP 2?</a>. How can I use JSP result to generate a JSON object? Is there a better way to return JSON object from JSP?</p>