Search code examples
javahtmljsprating-system

How to create rating system in JSP?


I'm doing one litle project with JSP for topic Library. I want to create a rating system for books in library when end-user view detail of book and rating for this book. Can anyone give hints or tutorials how to go about this?


Solution

  • I'd suggest to use the jQuery Star Rating plugin for this. Check the demo page how it all look like. The JSP/HTML basically look like this (you only need to put the necessary JS/CSS/image files in the public webcontent). The magic is done by giving the radio buttons the class name star.

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Star rating demo</title>
            <link rel="stylesheet" href="jquery.rating.css">
            <script src="jquery.js"></script>
            <script src="jquery.rating.js"></script>
        </head>
        <body>
            <form>
                <input type="radio" name="rating" value="1" class="star">
                <input type="radio" name="rating" value="2" class="star">
                <input type="radio" name="rating" value="3" class="star">
                <input type="radio" name="rating" value="4" class="star">
                <input type="radio" name="rating" value="5" class="star">
            </form>
        </body>
    </html>
    

    In the server side you just use HttpServletRequest#getParameter() to obtain the rating value.

    String rating = request.getParameter("rating");
    // ...
    

    With the above example, it'll return 1, 2, 3, 4 or 5.