Commit 6275f254 by Agrifina

Project Servlet JSP

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_91">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v9.0 (2)">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SimpleJspServletDB</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="WebContent"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="SimpleJspServletDB">
<wb-resource deploy-path="/" source-path="/WebContent" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<property name="context-root" value="SimpleJspServletDB"/>
<property name="java-output-path" value="/SimpleJspServletDB/build/classes"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v9.0 (2)"/>
<fixed facet="wst.jsdt.web"/>
<fixed facet="java"/>
<fixed facet="jst.web"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.web" version="3.1"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Book.do" method="post">
<fieldset>
<div>
<label for="bookId">Book Id</label> <input type="text"
name="bookId" value="<c:out value="${book.bookId}" />"
readonly="readonly" placeholder="Book Id" />
</div>
<div>
<label for="title">Book Title</label> <input type="text"
name="title" value="<c:out value="${book.title}" />"
placeholder="Book Title" />
</div>
<div>
<label for="author">Author</label> <input type="text"
name="author" value="<c:out value="${book.author}" />"
placeholder="Author" />
</div>
<div>
<label for="isbn">ISBN</label> <input type="text" name="isbn"
value="<c:out value="${buku.isbn}" />" placeholder="ISBN" />
</div>
<div>
<label for="stock">Stock</label> <input type="text" name="stock"
value="<c:out value="${book.stock}" />" placeholder="Stock" />
</div>
<div>
<input type="submit" value="Submit" />
</div>
</fieldset>
</form>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Book Id</th>
<th>Title</th>
<th>Author</th>
<th>ISBN</th>
<th>Stock</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${books}" var="book">
<tr>
<td><c:out value="${book.bookId}" /></td>
<td><c:out value="${book.title}" /></td>
<td><c:out value="${book.author}" /></td>
<td><c:out value="${book.isbn}" /></td>
<td><c:out value="${book.stock}" /></td>
<td><a
href="Book.do?action=edit&bookId=<c:out value="${book.bookId}"/>">Update</a></td>
<td><a
href="Book.do?action=delete&bookId=<c:out value="${book.bookId}"/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<p>
<a href="Book.do?action=insert">Add Book</a>
</p>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="/Book?action=bookList" />
</body>
</html>
\ No newline at end of file
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/library
user=root
password=""
\ No newline at end of file
create database LibraryDB;
use LibraryDB;
grant all on LibraryDB.* to 'admin'@'localhost' identified by 'test';
CREATE TABLE LibraryDB.`book` (
`bookId` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(45) DEFAULT NULL,
`author` varchar(45) DEFAULT NULL,
`isbn` varchar(100) DEFAULT NULL,
`stock` int(11) DEFAULT NULL,
PRIMARY KEY (`bookId`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
\ No newline at end of file
package com.library.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 com.library.dao.BookDao;
import com.library.dao.BookDaoImplementation;
import com.library.model.Book;
@WebServlet("/Book")
public class BookController extends HttpServlet
{
private BookDao dao;
private static final long serialVersionUID = 1L;
public static final String BOOK_LIST = "/bookList.jsp";
public static final String INSERT_OR_EDIT = "/book.jsp";
public BookController() {
dao = new BookDaoImplementation();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String forward = "";
String action = request.getParameter( "action" );
if( action.equalsIgnoreCase( "delete" ) ) {
forward = BOOK_LIST;
int bookId = Integer.parseInt( request.getParameter("bookId") );
dao.deleteBook(bookId);
request.setAttribute("books", dao.getAllBooks() );
}
else if( action.equalsIgnoreCase( "edit" ) ) {
forward = INSERT_OR_EDIT;
int bookId = Integer.parseInt( request.getParameter("bookId") );
Book book = dao.getBookById(bookId);
request.setAttribute("book", book);
}
else if( action.equalsIgnoreCase( "insert" ) ) {
forward = INSERT_OR_EDIT;
}
else {
forward = BOOK_LIST;
request.setAttribute("books", dao.getAllBooks() );
}
RequestDispatcher view = request.getRequestDispatcher( forward );
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Book book = new Book();
book.setTitle(request.getParameter("title"));
book.setAuthor(request.getParameter("author"));
book.setIsbn(request.getParameter("isbn"));
book.setStock(Integer.parseInt(request.getParameter("stock")));
String bookId = request.getParameter("bookId");
if(bookId == null || bookId.isEmpty() )
dao.addBook(book);
else
{
book.setBookId(Integer.parseInt(bookId));
dao.updateBook(book);
}
RequestDispatcher view = request.getRequestDispatcher(BOOK_LIST);
request.setAttribute("students", dao.getAllBooks());
view.forward(request, response);
}
}
package com.library.dao;
import java.util.List;
import com.library.model.Book;
public interface BookDao
{
public void addBook(Book book);
public void deleteBook(int bookId);
public void updateBook(Book book);
public List<Book> getAllBooks();
public Book getBookById(int bookId);
}
package com.library.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.library.model.Book;
import com.library.util.DbUtil;
public class BookDaoImplementation implements BookDao
{
private Connection connect;
public BookDaoImplementation()
{
connect = DbUtil.getConnection();
}
@Override
public void addBook(Book book)
{
try
{
String query = "insert into book (title, author, isbn, stock) values (?,?,?,?)";
PreparedStatement preparedStatement = connect.prepareStatement(query);
preparedStatement.setString(1, book.getTitle());
preparedStatement.setString(2, book.getAuthor());
preparedStatement.setString(3, book.getIsbn());
preparedStatement.setInt(4, book.getStock());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void deleteBook(int bookId)
{
try
{
String query = "delete from book where bookId=?";
PreparedStatement preparedStatement = connect.prepareStatement(query);
preparedStatement.setInt(1, bookId);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void updateBook(Book book)
{
try
{
String query = "update book set title=?, author=?, isbn=?, stock=? where isBook=?";
PreparedStatement preparedStatement = connect.prepareStatement(query);
preparedStatement.setString(1, book.getTitle());
preparedStatement.setString(2, book.getAuthor());
preparedStatement.setString(3, book.getIsbn());
preparedStatement.setInt(4, book.getStock());
preparedStatement.setInt(5, book.getBookId());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public List<Book> getAllBooks()
{
List<Book> books = new ArrayList<Book>();
try {
Statement statement = connect.createStatement();
ResultSet resultSet = statement.executeQuery( "select * from book" );
while( resultSet.next() ) {
Book book = new Book();
book.setBookId( resultSet.getInt( "bookId" ) );
book.setTitle( resultSet.getString( "title" ) );
book.setAuthor( resultSet.getString( "author" ) );
book.setIsbn( resultSet.getString( "isbn" ) );
book.setStock( resultSet.getInt( "stock" ) );
books.add(book);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
@Override
public Book getBookById(int bookId)
{
Book book = new Book();
try
{
String query = "select * from book where bookId=?";
PreparedStatement preparedStatement = connect.prepareStatement(query);
preparedStatement.setInt(1, bookId);
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next())
{
book.setBookId(resultSet.getInt("bookId"));
book.setTitle(resultSet.getString("title"));
book.setAuthor(resultSet.getString("author"));
book.setIsbn(resultSet.getString("isbn"));
book.setStock( resultSet.getInt("stock"));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
return book;
}
}
package com.library.model;
public class Book {
private int bookId;
private String title;
private String author;
private String isbn;
private int stock;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", title=" + title + ", author=" + author + ", isbn="
+ isbn + ", stock=" + stock + "]";
}
}
package com.library.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DbUtil
{
private static Connection connect;
public static Connection getConnection()
{
if( connect != null )
return connect;
InputStream inputStream = DbUtil.class.getClassLoader().getResourceAsStream( "/db.properties" );
Properties properties = new Properties();
try
{
properties.load( inputStream );
String driver = properties.getProperty( "driver" );
String url = properties.getProperty( "url" );
String user = properties.getProperty( "user" );
String password = properties.getProperty( "password" );
Class.forName( driver );
connect = DriverManager.getConnection( url, user, password );
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return connect;
}
public static void closeConnection( Connection toBeClosed )
{
if( toBeClosed == null )
return;
try
{
toBeClosed.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
\ No newline at end of file
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/library
user=root
password=""
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment