0

MySQL DB server used. Database name: library, table name: book. table contains the following columns with given type and order:

title(varchar), author(varchar), book_id(int)- primary key, count (int), favs (int).

I want to view all the records of this table using ArrayList in servlet and JSP. Here are my codes: ViewBook (servlet)

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import p1.*;
public class ViewBook extends HttpServlet
{
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
    PrintWriter out=res.getWriter();    
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/library", "root", "admin");
    PreparedStatement ps=con.prepareStatement("select * from book");
    ResultSet rs=ps.executeQuery();
    ArrayList<Book> books=new ArrayList<Book>();
    while(rs.next())
    {
        Book b= new Book();
        b.bookID=rs.getInt(3);
        b.bookTitle=rs.getString(1);
        b.bookAuthor=rs.getString(2);
        b.bookCopies=rs.getInt(4);
        b.bookFavs=rs.getInt(5);
        books.add(b);
    }
    req.setAttribute("bookslist",books);
    con.close();

    RequestDispatcher rd=req.getRequestDispatcher("/view_book.jsp");
    rd.forward(req,res);

    }
    catch(Exception e)
    {
        out.println(e);
    }
}

}

Package p1 contains the following class:

Book.java

package p1;

public class Book
{
    public int bookFavs,bookID, bookCopies;
    public String bookTitle;
    public String bookAuthor;

}

The JSP to which the request is dispatched- view_book.jsp:

<html>
    <body>
    <head>
    <title>
    View Books
    </title>
    </head>
    <body>
    <table border=2>
    <tr>
        <th>Book ID</th>
        <th>Title</th>
        <th>Author</th>
        <th>No. of copies  AVAILABLE</th>
        <th>Number of favourites</th>
    </tr>

    <%
        ArrayList<Book> dbooks=(ArrayList)request.getAttribute("bookslist");
        Iterator it=dbooks.iterator();
        while(it.hasNext())
        {
            Book b=(Book)it.next();
    %>
    <tr>
        <td><%=b.bookID%></td>
        <td><%=b.bookTitle%></td>
        <td><%=b.bookAuthor%></td>
        <td><%=b.bookCopies%></td>
        <td><%=b.bookFavs%></td>    
    </tr>
    <%
        }
    %>
    </table>

    </body>
</html>

The servlet and JSP aren't working. I know it's a vague question. Please point the blunders made. And solutions if possible!

Srinu Chinka
  • 1,471
  • 13
  • 19
  • 1
    What is "aren't working"? Are there errors in the server log? Errors in the display? Wrong output? – RealSkeptic Aug 27 '15 at 19:43
  • We need to know the exact errors you're dealing with, so we can help. Are you connecting to the DB but pulling the wrong information? – Sterls Aug 27 '15 at 19:47
  • java.lang.ClassNotFoundException: org.apache.jsp.view_005fbook_jsp exception: org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.view_005fbook_jsp org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:176) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) – Gayathri Keerthana Aug 27 '15 at 20:00
  • First of all, I need to know if the code is logically correct. Servlet is compiling. Jsp is not. – Gayathri Keerthana Aug 27 '15 at 20:03

4 Answers4

0

The JSP file cant compile because the ArrayList needs an import directive :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
chenchuk
  • 5,324
  • 4
  • 34
  • 41
0

You have to import your Book and ArrayList and Iterator in your JSP. Otherwise your JSP file will not compile.

you can import multiple classes by using , instead of ; like in the java. If you specify ; then you JSP will throw Exception.

<%@ page import="java.util.ArrayList,java.util.Iterator, p1.Book" %>
Srinu Chinka
  • 1,471
  • 13
  • 19
0

Import java.util.*;to import the ArrayList class in the jsp. Then, also import the required classes e.g. the Book class too. It should work.

mnille
  • 1,328
  • 4
  • 16
  • 20
Mwangi Thiga
  • 1,339
  • 18
  • 22
0

The problem is quite simple: you don't execute the servlet anytime. You must to invoke the servlet from another .jsp then, inside that servlet, call the other jsp.

ItsPete
  • 2,363
  • 3
  • 27
  • 35