1. Reading the Images from mysql database into the JSP page using java:

Reading the Images from the database into the JSP page: 

https://www.codejava.net/coding/how-to-display-images-from-database-in-jsp-page-with-java-servlet


mysqldump --no-data --skip-comments --user=root --password=root --host=localhost test book > D:\vm\book.sql

DATABASE: 
===============================================================================================================

DROP TABLE IF EXISTS `book`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `book` (
  `book_id` int NOT NULL AUTO_INCREMENT,
  `title` varchar(128) DEFAULT NULL,
  `author` varchar(64) DEFAULT NULL,
  `image` longblob,
  PRIMARY KEY (`book_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;



MODEL
================================================================================================================




package net.codejava;

public class Book {
private String title;
private String author;
private String base64Image;
public String getBase64Image() {
return base64Image;
}
public void setBase64Image(String base64Image) {
this.base64Image = base64Image;
}
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;
}
// other fields...
// other getters and setters…
}


DAO
===============================================================================================================

package net.codejava.dao;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Base64;
import net.codejava.Book;

public class BookDAO {
String databaseURL = "jdbc:mysql://localhost:3306/test?characterEncoding=latin1";
String user = "root";
String password = "root";

public Book get(int id) throws SQLException, IOException {
Book book = null;

String sql = "SELECT * FROM book WHERE book_id = ?";

try (Connection connection = DriverManager.getConnection(databaseURL, user, password)) {
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet result = statement.executeQuery();

if (result.next()) {
book = new Book();
String title = result.getString("title");
String author = result.getString("author");
System.out.println("title" + title);
System.out.println("author" + author);
Blob blob = result.getBlob("image");
InputStream inputStream = blob.getBinaryStream();
System.out.println("Vaue of inputStream is" + inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.out.println("I am from after inputStream");
byte[] buffer = new byte[4096];
int bytesRead = -1;

while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

byte[] imageBytes = outputStream.toByteArray();
String base64Image = Base64.getEncoder().encodeToString(imageBytes);

inputStream.close();
outputStream.close();

book.setTitle(title);
book.setAuthor(author);
book.setBase64Image(base64Image);
}

} catch (SQLException | IOException ex) {
ex.printStackTrace();
throw ex;
}

return book;
}
}



CONTROLLER
===============================================================================================================


package net.codejava.controller;

import java.io.IOException;
import java.sql.SQLException;

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 net.codejava.dao.BookDAO;
import net.codejava.Book;

@WebServlet("/test")
public class GetBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public GetBookServlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

int bookId = Integer.parseInt(request.getParameter("id"));
BookDAO dao = new BookDAO();

try {
Book book = dao.get(bookId);

System.out.println("Value of the book is " + book);

request.setAttribute("book", book);

String page = "/index.jsp";
RequestDispatcher requestDispatcher = request.getRequestDispatcher(page);
requestDispatcher.forward(request, response);
} catch (SQLException ex) {
throw new ServletException(ex);
}

}
}


VIEW
===============================================================================================================

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<body>
<a href="servlet1">click for photo</a>

<form action="./test">
<div align="center">
<h2>
<c:out value="${book.title}" />
</h2>
<h3>
<c:out value="${book.author}" />
</h3>
<img src="data:image/jpg;base64,${book.base64Image}" width="240"
height="300" /> <input type="submit" value="Submit">
</div>

</form>


</body>
</html>


===============================================================================================================

No comments:

Post a Comment