Issue
I'm trying to create Java web application using tomcat 8 server in eclipse. I've created servlet which forwards request to jsp page and initialize table inside this page with data from two tables. I've been following several examples on the internet on how to fill html table with recoreds from MySQL DB but failed. I'm using MainServlet's doGet method to get data from my local database into list and set it as attribute in request. But when i'm launching servlet there are no records in jsp table. Servlet connects to db and recieves records from it (i've already debugged app). So what is wrong with my code? I'm new to html and css,servlet and jsp technology. Maybe problem is with css framework i'm using (Materialize CSS). Here is my MainServlet.java code:
package redirect;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import util.Book;
@WebServlet(name="Libbook",urlPatterns={"/libbook"})
public class MainServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private static String url = "jdbc:mysql://localhost:3306/Test";
private static String user = "user",password = "password";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List<Book> books = new ArrayList<>();
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url,user,password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
"SELECT IDBook,Title,AddDate,ISBN,Name,Sirname "
+ "FROM Book INNER JOIN Author "
+ "ON Book.IDAuthor = Author.IDAuthor;");
while (resultSet.next())
{
Book b = new Book();
b.setId(resultSet.getInt(1));
b.setTitle(resultSet.getString(2));
b.setIsbn(resultSet.getLong(4));
b.setDate(resultSet.getDate(3).toString());
b.setName(resultSet.getString(5));
b.setSirname(resultSet.getString(6));
books.add(b);
}
}
catch (SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
finally {
try {if(connection != null)connection.close();}catch (SQLException e) {e.printStackTrace();}
}
request.setAttribute("books", books);
request.getRequestDispatcher("pages/libbook.jsp").forward(request, response);
}
}
libbook.jsp page:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8"/>
<title>LibBook</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<style type="text/css">
.logo{
color: #444;
text-transform: uppercase;
letter-spacing: 0.3em;
font-size: 2em;
}
.logo img{
width: 5%;
margin: 0.5%;
vertical-align: middle;
margin-right: 0.5em;
}
</style>
</head>
<body>
<nav class="white">
<div class="navbar-wrapper container">
<a id="logo-container" href="#" class="logo">
LIBBOOK
<img src="assets/book.svg" alt="LibBooklogo">
</a>
<ul class="right">
<li><a class="waves-effect waves-light btn" href="pages/new_author.jsp"><i class="material-icons left">perm_identity</i>NEW AUTHOR</a></li>
<li><a class="waves-effect waves-light btn" href="pages/new_book.jsp"><i class="material-icons left">class</i>NEW BOOK</a></li>
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="card-panel">
<form action="test">
<div class="input-field">
<input placeholder="Type here (title,author) and press enter.." id="search_bar" type="text">
<label for="search_bar">Search</label>
</div>
</form>
</div>
</div>
<div class="row">
<div class="card-panel">
<table>
<thead>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Info/Edit</th>
<th>Remove</th>
</tr>
</thead>
<c:forEach var="book" items="${books}">
<tr>
<td><c:out value="${book.isbn}"/></td>
<td><c:out value="${book.title}"/></td>
<td><c:out value="${book.name}"/></td>
<td>
<a href=""><i class="material-icons">mode_edit</i></a>
</td>
<td>
<a href=""><i class="material-icons">delete</i></a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js"></script>
</body>
</html>
Book class:
package util;
import java.io.Serializable;
public class Book implements Serializable
{
private Integer id;
private String title;
private String date;
private Long isbn;
private String name;
private String sirname;
public Book(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Long getIsbn() {
return isbn;
}
public void setIsbn(Long isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSirname() {
return sirname;
}
public void setSirname(String sirname) {
this.sirname = sirname;
}
}
Solution
Found solution. Problem was in jsp file, i did not include imports related to c tag scriplet:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
And didn't add jstl-1.2.jar library to my project
Answered By - devaerial
Answer Checked By - Senaida (JavaFixing Volunteer)