Upload Paradigm to database and Display Image on JSP folio dynamically using JSP & Servlet.

For a complete tutorial on How to Upload images to the database and Display Images on the JSP folio dynamically using JSP & Servlet?

Cheque OUT Below VIDEO

Practise SUBSCRIBE MY YOUTUBE CHANNEL FOR More TUTORIALS.

Program TO Upload Paradigm to database and Brandish Paradigm on JSP page dynamically using JSP & Servlet.

SOURCE CODE :

addImage.jsp

<%@ folio language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-ane"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Add Image</title>

</head>

<body>

<h1 way="color:cerise" align="center">ADD Image Item</h1>

<div align="center">

<form action="AddImage" method="post" enctype="multipart/form-data">

   Select Image :

   <input type="file" name="image">

   <input type="submit" value="Add together Image">

</form>

</div>

</body>

</html>

AddImage.java (Servlet)

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.sql.*;

import javax.servlet.ServletException;

import javax.servlet.annotation.MultipartConfig;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.Function;

@MultipartConfig

@WebServlet("/AddImage")

public class AddImage extends HttpServlet {

private static concluding long serialVersionUID = 1L;

    public AddImage() {

        super();

    }

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

// TODO Motorcar-generated method stub

response.getWriter().append("Served at: ").append(asking.getContextPath());

}

protected void doPost(HttpServletRequest asking, HttpServletResponse response) throws ServletException, IOException {

Organisation.out.println("In practise post method of Add together Image servlet.");

Part file=asking.getPart("image");

String imageFileName=file.getSubmittedFileName();  // go selected epitome file proper noun

Arrangement.out.println("Selected Paradigm File Name : "+imageFileName);

String uploadPath="C:/Users/Vostro.MURADALIMJ/Desktop/Youtube Tutorials/ImageTutorial/WebContent/images/"+imageFileName;  // upload path where we have to upload our actual prototype

Organisation.out.println("Upload Path : "+uploadPath);

// Uploading our selected image into the images folder

attempt

{

FileOutputStream fos=new FileOutputStream(uploadPath);

InputStream is=file.getInputStream();

byte[] data=new byte[is.available()];

is.read(information);

fos.write(information);

fos.close();

}

catch(Exception e)

{

e.printStackTrace();

}

//**********************

//getting database connexion (jdbc code)

Connection connection=zilch;

attempt

{

Class.forName("com.mysql.cj.jdbc.Driver");

connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/imageTutorial","root","your password");

PreparedStatement stmt;

String query="insert into image(imageFileName) values(?)";

stmt=connection.prepareStatement(query);

stmt.setString(1,imageFileName);

int row=stmt.executeUpdate(); // it returns no of rows affected.

if(row>0)

{

System.out.println("Image added into database successfully.");

}

else

{

Organisation.out.println("Failed to upload paradigm.");

}

}

catch (Exception e)

{

System.out.println(due east);

}

}

}

displayImage.jsp

<%@ page language="coffee" contentType="text/html; charset=ISO-8859-i"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Display Prototype</championship>

</head>

<trunk>

<h1 style="colour:cherry" align="middle">DISPLAY IMAGE Particular</h1>

<div align="eye">

<form action="DisplayServlet" method="post">

   Enter Prototype Id :

   <input type="number" name="imageId">

   <input type="submit" value="Display Prototype">

</form>

</div>

<hr>

<%

    String imgFileName=(String)request.getAttribute("img");

    String imgId=(Cord)asking.getAttribute("id");

    System.out.println(imgFileName);

%>

<div align="center">

     <table border="5px" way="width:600px">

          <tr>

              <th>Image Id </thursday>

              <th>Image</th>

          </tr>

         <%

             if(imgFileName!="" && imgId!="")

             {

         %>

          <tr>

              <td><%=imgId %></td>

              <td><img src="images/<%=imgFileName%>" mode="width:300px;height:250px"></td>

          </tr>

        <%

             }

        %>

     </table>

</div>

</body>

</html>

AddImage.java (Servlet)

import coffee.io.IOException;

import java.sql.Connectedness;

import coffee.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

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;

@WebServlet("/DisplayServlet")

public class DisplayServlet extends HttpServlet {

individual static final long serialVersionUID = 1L;

    public DisplayServlet() {

        super();

    }

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

response.getWriter().append("Served at: ").append(request.getContextPath());

}

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

Organisation.out.println("In do mail method of Display Prototype servlet.");

String imageId=request.getParameter("imageId");

int id=Integer.parseInt(imageId);

//getting database connection (jdbc code)

Connectedness connectedness=null;

int imgId=0;

String imgFileName=null;

effort

{

Grade.forName("com.mysql.cj.jdbc.Driver");

connectedness=DriverManager.getConnection("jdbc:mysql://localhost:3306/imageTutorial","root","your password");

Statement stmt;

String query="select * from epitome";

stmt=connection.createStatement();

ResultSet rs=stmt.executeQuery(query);

while(rs.side by side())

{

if(rs.getInt("imageId")==id)

{

imgId=rs.getInt("imageId");

imgFileName=rs.getString("imageFileName");

}

}

}

catch (Exception due east)

{

System.out.println(due east);

}

RequestDispatcher rd;

asking.setAttribute("id",Cord.valueOf(imgId));  //valueOf

asking.setAttribute("img",imgFileName);

rd=request.getRequestDispatcher("displayImage.jsp");

rd.forward(request, response);

}

}

Epitome REFERENCED IN THE VIDEO FOR EXPLAINING THE CONCEPTS.

****************IMPORTANT**********

IF YOU ARE FACING ANY PROBLEM DO CONTACT ME

Mail ME AT @mjmuradali31@gmail.com

OR

DM ME ON INSTAGRAM at@ muradalimj (👈👈CLICK HERE)

OR

DM ME ON FACEBOOK at Murad Ali ( 👈👈 CLICK Hither)

******************************************

Thank you:)

CODE WITH MURAD