Vishal Anand
Vishal Anand

Reputation: 591

how to link to dynamically generated page in jsp

I am new to server side programming. I have a final year project on Online Book sales management system. I want to know how to generate a dynamic page for each of book images clicked for purchase. I mean to say that if user requests for any book X, how is it possible to generate a page which shows its all details, image etc information. what kind of url should I use?

Upvotes: 2

Views: 5113

Answers (2)

SaK
SaK

Reputation: 420

I am assuming it's a small scale project.
First maintain a database for your books.. give each of 'em an unique id. If you are display all the common details like... published on, price, book name, author, reviews, rating.... Then you don't need to worry.. Create a common JSP Page to display the indo based on an uniqueId...

jsp say: books.jsp

<form name="books" action="getInfo.jsp">
    <a href="YourJSP/?bookid=pass_an_id_for_your_bookX">Book X(Image)</a> //Book X
    <a href="YourJSP/?bookid=pass_an_id_for_your_bookY">Book Y(Image)</a> //Book Y 
    <a href="YourJSP/?bookid=pass_an_id_for_your_bookZ">Book Z(Image)</a> //Book Z
....
.... an on..
</form>

?bookid will be your paramid.
pass_an_id_for_your_bookX will be your unique id of your bookX stored in database.

inside your 2nd JSP say: getInfo.jsp

Design your JSP for presentation, to diplay info about your BookX / BookY w.r.t to bookId In your JSP get the value of bookId and query the DB...

        String bookId = request.getParameter("bookid"):
        //This will get the value of your bookId for BookX

        //Connect to your DB
        //Use PreparedStatement or StoredProcedure to make a query pass your bookId in where condition. 

Rest is simple, By Querying the database you will all the usual values ie. published on, price, book name, author, reviews, rating... and display them accordingly in your JSP page..

      <%
      Connection con = null;
      PreparedStatement pst = null;
      ResultSet rs = null;

      String BookName=null;
      String AuthorName=null;
      String Price=null;
      String Rating=null;
      try {

      Class.forName(driver);
      con = DriverManager.getConnection(connection);

      String sql =
      "select * from BOOKS_TABLE where bookId =?";
      pst = con.prepareStatement(sql);
      pst.setString(1, bookId);

      rs = pst.executeQuery();

      while (rs.next()) {
      BookName = rs.getString(1);
      AuthorName = rs.getString(2);
      Price = rs.getString(3);
      Rating = rs.getString(4);

      }
      // 1 , 2, 3... denotes column numbers
      } catch (SQLException e) {
      System.out.println(e.getMessage());
      }
      }
    } 
    %>

Now you got all your values.. Well, display them accordingly..

       <%=BookName%>
       <%=AuthorName%>
       <%=Price%>
       <%=Rating%>

A Note: Scriplets(Java Code) are not encouraged in JSP page.. You may want to check into Beans or JSTL. I'm just giving you an IDEA!

ALL THE BEST

Upvotes: 3

BalusC
BalusC

Reputation: 1108632

Just pass the unique identifier of the book as request parameter or request path info and have a servlet to perform the data lookup in the doGet() method based on the identifier, store it in the request scope and forward to a JSP file which generates all the necessary HTML.

E.g. as request parameter /book?id=123 with this servlet

@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);
    }

}

Or as request path info /book/123 with this servlet

@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);
    }

}

Either way, the /WEB-INF/book.jsp could just look like this the usual way.

<p>Title: <c:out value="${book.title}" /></p>
<p>Author: <c:out value="${book.author}" /></p>
<p>Summary: <c:out value="${book.summary}" /></p>

In order to generate those links to the servlet, just loop over a List<Book> in /WEB-INF/books.jsp as follows, assuming that you want URLs like /book/123:

<ul>
    <c:forEach items="${books}" var="book">
        <li>
            <a href="${pageContext.request.contextPath}/book/${book.id}">
                View book with title: <c:out value="${book.title}" />
            </a>
        </li>
    </c:forEach>
</ul>

Upvotes: 3

Related Questions