Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just pass the unique identifier of the book as request parameter or request path info and have a <a href="https://stackoverflow.com/tags/servlets/info">servlet</a> to perform the data lookup in the <code>doGet()</code> method based on the identifier, store it in the request scope and forward to a JSP file which generates all the necessary HTML.</p> <p>E.g. as request parameter <code>/book?id=123</code> with this servlet</p> <pre><code>@WebServlet("/book") public class BookServlet extends HttpServlet { @EJB private BookService service; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Book book = service.find(request.getParameter("id")); request.setAttribute("book", book); request.getRequestDispatcher("/WEB-INF/book.jsp").forward(request, response); } } </code></pre> <p>Or as request path info <code>/book/123</code> with this servlet</p> <pre><code>@WebServlet("/book/*") public class BookServlet extends HttpServlet { @EJB private BookService service; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Book book = service.find(request.getPathInfo().substring(1)); request.setAttribute("book", book); request.getRequestDispatcher("/WEB-INF/book.jsp").forward(request, response); } } </code></pre> <p>Either way, the <code>/WEB-INF/book.jsp</code> could just look like this the usual way.</p> <pre><code>&lt;p&gt;Title: &lt;c:out value="${book.title}" /&gt;&lt;/p&gt; &lt;p&gt;Author: &lt;c:out value="${book.author}" /&gt;&lt;/p&gt; &lt;p&gt;Summary: &lt;c:out value="${book.summary}" /&gt;&lt;/p&gt; </code></pre> <p>In order to generate those links to the servlet, just loop over a <code>List&lt;Book&gt;</code> in <code>/WEB-INF/books.jsp</code> as follows, assuming that you want URLs like <code>/book/123</code>: </p> <pre><code>&lt;ul&gt; &lt;c:forEach items="${books}" var="book"&gt; &lt;li&gt; &lt;a href="${pageContext.request.contextPath}/book/${book.id}"&gt; View book with title: &lt;c:out value="${book.title}" /&gt; &lt;/a&gt; &lt;/li&gt; &lt;/c:forEach&gt; &lt;/ul&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload