0

I have a created a small Java web application. I have made use of DAO and a JSP file. My issue is that when I enter an isbn number in the input field, if the book is found in the database, it displays me "Book Found" and the corresponding details. But if I enter an isbn number in the input field, and if the book is not found in the db, it still displays me "Book Found" but with isbn number 0 and book title "null" which I don't want. In that case, it should display me only "Book not found".

Note that the problem lies mainly in the JSP page.

Here is my full code: http://pastebin.com/cTZy4w6V (using pastebin since the code is too long)

Here is the JSP code:

<jsp:useBean id = "bm" class="book.ManagerBook" scope = "session"/>    

    <h1> Welcome to ABC Library</h1>

    <form>
            <table>
                    <tr>
                            <td> Enter Details </td>
                            <td><input type="text" name="isbn"></td>
                            <td><input type="submit" name="find" value="find"></td>
                    </tr>
            </table>
                    <input type="hidden" name="submitted" value="true">
            </form>


            <%     
                    Boolean submitted = Boolean.parseBoolean(request.getParameter("submitted"));
                    if(submitted){
                    int isbn = Integer.parseInt(request.getParameter("isbn"));     

                    Book b2 = bm.findBook(isbn);           

                    %>
                    <table>
                    <tr>
                    <td colspan=2>
                    <h2>Book Found</h2>
                    </td>
                    </tr>

                    <tr>
                            <td><h3>ISBN</h3></td>
                            <td><h3>Title</h3></td>
                    </tr>  

                    <tr>
                            <td><%= b2.getIsbn()%></td>
                            <td><%= b2.getTitle() %></td>
                    </tr>
                    </table>

                    <%}else if(!submitted){ %>
                    <h3> Book Not Found</h3>
                    <% } %>
James Smith
  • 55
  • 2
  • 11

1 Answers1

0

If possible, don't use scriptlets in jsp. It's a bad practice.

Try something like this:

                    if(submitted){
                    int isbn = Integer.parseInt(request.getParameter("isbn"));     

                    Book b2 = bm.findBook(isbn);           
                    if (b2.getTitle() != null) {
                    %>
                    <table>
                    <tr>
                    <td colspan=2>
                    <h2>Book Found</h2>
                    </td>
                    </tr>

                    <tr>
                            <td><h3>ISBN</h3></td>
                            <td><h3>Title</h3></td>
                    </tr>  

                    <tr>
                            <td><%= b2.getIsbn()%></td>
                            <td><%= b2.getTitle() %></td>
                    </tr>
                    </table>

                    <% }  else { %>
                    <h3> Book Not Found</h3>
                    <% } %> }
user2953113
  • 545
  • 4
  • 16
  • Thanks man ! Working perfectly.. :) If not using scriplets in jsp, what do you recommend ? :) – James Smith Sep 20 '15 at 14:57
  • Check this [WhyNotUseScriptlets](http://www.coderanch.com/how-to/java/WhyNotUseScriptlets) and [how-to-avoid-java-code-in-jsp-files](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) – user2953113 Sep 20 '15 at 17:29