Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Image uploading and saving into MySQL and then display image

Let us look at how to upload an image into MySQL DB and then display the image using JSP, Servlet and MySQL database.

Java Servlet API doesn't provide any method to handle file upload. So, we have to depend on a library provided by third parties. In this post I am going to use Commons Fileupload library provided by Apache Foundation.


•    commons-io-2.0.jar ( http://commons.apache.org/io/download_io.cgi )
•    commons-fileupload-1.2.2.jar ( http://commons.apache.org/fileupload/download_fileupload.cgi )

Now let us starts with a JSP file which has the following fields First Name, Last Name and Portrait Photo.

Upload.jsp
<%--
 *
 * Author           : Sanjeev Kulkarni
 * Project          : WebLearning
 * Created On       : 03-Jul-2013 3:12:44 PM
 *
--%>


<%@ 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>File Upload to Database Demo</title>
</head>
<body>
    <center>
        <h1>File Upload to Database Demo</h1>
        <form action="displayServlet" method="get">
            <table>
                <tr>
                    <td>
                        <div style="background: none repeat scroll 0 0 ghostwhite; height: 150px; width: 150px;">
                            <img src="displayServlet?id=13" height="150px" width="150px" alt="ProfilePic">
                    </div>
                    </td>
                </tr>
            </table>
        </form>
        <form method="post" action="uploadServlet" enctype="multipart/form-data">
            <table border="0">
                <tr>
                    <td>First Name: </td>
                    <td><input type="text" name="firstName" size="50"/></td>
                </tr>
                <tr>
                    <td>Last Name: </td>
                    <td><input type="text" name="lastName" size="50"/></td>
                </tr>
                <tr>
                    <td>Portrait Photo: </td>
                    <td><input type="file" name="photo" size="50"/></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="Save">
                    </td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>


Once you upload the image and click on Save button, request will be sent to FileUploadDBServlet which process the request and stores the image into MySQL db.

FileUploadDBServlet.java 
 
package com.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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.Part;

/**
 * Servlet implementation class FileUploadDBServlet
 */
@WebServlet("/FileUploadDBServlet")
@MultipartConfig(maxFileSize = 10177215) // upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileUploadDBServlet() {
        super();
    }

    private String dbURL = "jdbc:mysql://localhost:3306/web_learning";
    private String dbUser = "root";
    private String dbPass = "root";
     
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // gets values of text fields
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
         
        InputStream inputStream = null; // input stream of the upload file
         
        // obtains the upload file part in this multipart request
        Part filePart = request.getPart("photo");
        if (filePart != null) {
            // prints out some information for debugging
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());
             
            // obtains input stream of the upload file
            inputStream = filePart.getInputStream();
        }
         
        Connection conn = null; // connection to the database
        String message = null;  // message will be sent back to client
         
        try {
            // connects to the database
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
 
            // constructs SQL statement
            String sql = "INSERT INTO contacts (first_name, last_name, photo) values (?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, firstName);
            statement.setString(2, lastName);
             
            if (inputStream != null) {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(3, inputStream);
            }
 
            // sends the statement to the database server
            int row = statement.executeUpdate();
            if (row > 0) {
                message = "File uploaded and saved into database";
            }
        } catch (SQLException ex) {
            message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            // sets the message in request scope
            request.setAttribute("Message", message);
             
            // forwards to the message page
            getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
        }
    }
}


So we are done with uploading of the image. So simple isn’t it! Now you have the image stored into the database, next question is how to retrieve the image? We know little bit about the TNIYBLOB,BLOB,MEDIUMBLOB,LONGBLOB and the content of these types are stored in the db as a binary object.

•    TINYBLOB: maximum length of 255 bytes
•    BLOB: maximum length of 65,535 bytes
•    MEDIUMBLOB: maximum length of 16,777,215 bytes
•    LONGBLOB: maximum length of 4,294,967,295 bytes



Note that if you generate your table from the JPA annotations, you can "control" the type MySQL will use by specifying the length attribute of the Column, for example:


@Lob 
@Basic(fetch = FetchType.LAZY)
@Column(length=100000)
private byte[] picture;


Depending on the length, you'll get:
       0 < length <=      255  -->  `TINYBLOB`
     255 < length <=    65535  -->  `BLOB`
   65535 < length <= 16777215  -->  `MEDIUMBLOB`
16777215 < length <=    2³¹-1  -->  `LONGBLOB`


In Open JPA if we do not provide FetchType, I was getting some warnings whenever I was running the enhancer tool for enhancing the entities. Now don’t worry about the JPA concept, we will concentrate just on servlet, jdbc.



DisplayServlet.java
 
package com.servlet;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DisplayServlet
 */
@WebServlet("/DisplayServlet")
public class DisplayServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    public void init() throws ServletException {

    }

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DisplayServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String imageId = request.getParameter("id");
        System.out.println(imageId);
        InputStream sImage;

        // Check if ID is supplied to the request.
        if (imageId == null) {
            // Do your thing if the ID is not supplied to the request.
            // Throw an exception, or send 404, or show default/warning image, or just ignore it.
            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
            return;
        }

        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web_learning", "root","root");
            stmt = conn.prepareStatement("select * from contacts where contact_id=" + imageId);
            rs = stmt.executeQuery();
            if(rs.next()){
                System.out.println("Inside RS");
                byte[] bytearray = new byte[1048576];
                int size=0;
                sImage = rs.getBinaryStream(4);
                response.reset();
                response.setContentType("image/jpeg");
                while((size = sImage.read(bytearray)) != -1 ){
                    response.getOutputStream().
                    write(bytearray,0,size);
                }
            }

        } catch (Exception e){
            e.printStackTrace();
        }
    }
}




For displaying image in any of your JSP page you need to pass the id like below:

<img src="displayServlet?id=1" height="150px" width="150px" alt="ProfilePic">

There is one more method for displaying the image from DB. You can refer to the following post for the same: http://balusc.blogspot.in/2007/04/imageservlet.html#ImageServletServingFromDatabase

Hope this post helps for your development activities. See you then,

Sanjeev Kulkarni

Learning OpenJPA step by step


 Hi Folks,

Java persistence framework is high on demand now days. If you mention JPA in your resume, it will catch hold of the interviewer and can influence in fetching you the job. (Ha ha I am not joking here! though some of you might be thinking :)-)

Ok lets come back to our topic JPA i.e. Java Persistence API.The Java Persistence API provides an object/relational mapping for managing relational data in enterprise beans, web components, and application clients. It can also be used in Java SE applications, outside of the Java EE environment.

Now we will try to understand practically how this will work.

1> Download and install latest eclipse IDE. In my case I have used Eclipse Indigo.
2> Download the latest apache tomcat. I have tomcat7.0
3> Once you have downloaded the tomcat extract it and keep it in a folder.
4> Now in eclipse IDE, select File > New > other and pick Server from the list.  

Click Next and select Tomcat v6.0 Server for the server definition. You'll need to
select a run time.

 Click on Add link and you will be prompted for the next window to add server runtime configurations.
Click on Browse button to add the location of tomcat directory where you have extracted the tomcat. Then click finish. Upon clicking finish it will take back you to previous window. In that also you click on Finish button.
Now you are done with server configurations. Now let’s start building a dynamic web project to learn JPA.
5> Click on File->New->Dynamic Web Project
Name your project and click on Next button.
No need to do anything, just click Next button.
Check generate web.xml checkbox and click on Finish.
Now you are done with creating dynamic web application! Congratulations! Now let’s add the required library files to the build path. Below are the required jar files.
ü       persistence-api-1.0.2.jar
ü       openjpa-all-2.2.0.jar
ü      mysql-connector-java-5.1.21-bin.jar (Off course I am using MySQL as my DB!)

To add these libraries to the build path of your project (off course you should download them before from the links provided and add them into you WEB-INF/lib location.) just right click on your project->Properties->Java Build Path.
Then click on add Add JARs button and then click on your project->Web Content->WEB-INF->lib then select the jars and click OK button.
Next go to Order and Export tab click on Select All button and then click on OK button.
6> Now let’s create our first entity. Note that we have used Java Annotations in this class.
Right click on the project->New->Class
Give the package name as com and class name as User and click on Finish button.

Now the contents of the class User is as below.

package com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

/**
@Author              : Sanjeev Kulkarni
@Created on          : 05-Oct-201212:25:13 PM
@Project             : a
@Package             :
@File                : User.java
@Comments            :

Revision History     :
Sl No. | Revision Date      | Changed By               | Reasons           
------------------------------------------------------------------------------------

 **/
@Entity(name="User")
public class User implements Serializable{
      
       private static final long serialVersionUID = 975983410088693889L;
      
       public User(){
             
       }

       @Column(name="id")
        @GeneratedValue(strategy=GenerationType.AUTO)

       private int id;
      
       @Column(name="name")
       private String name;

       public int getId() {
              return id;
       }

       public void setId(int id) {
              this.id = id;
       }

       public String getName() {
              return name;
       }
      

       public void setName(String name) {
              this.name = name;
       }
}


Now lets create a JSP page to print list of users from the database using openjpa.

Right click on project->New->Other->Web->JSP File and then click on Next button. Give the name of the file as NewFile.jsp and click Finish button.

Now copy and paste the content of NewFile.jsp as below.


<%@ 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">
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@ page import="com.User" %>
<%@page import="javax.persistence.Persistence"%>
<%@page import= "javax.persistence.Query"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>::</title>
</head>
<body>

<%
 final EntityManagerFactory emf;
 final EntityManager em;
 final String PERSISTENCE_UNIT_NAME = "projectjpa";

try{
 emf = Persistence.createEntityManagerFactory("projectjpa");
 em = emf.createEntityManager();

 em.getTransaction().begin();

 User user = new User();
 Query query = em.createQuery("select x from User x ");

 out.println("Query returned: " + query.getResultList().size());
 em.getTransaction().commit();
 em.close();
 emf.close();
}
catch(Exception e){
       out.println(e);
       e.printStackTrace();
      
}

 %>
</body>
</html>


Modify the web.xml descriptor file. Mention NewFile.jsp as a welcome file.


xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LearningOpneJPA</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>NewFile.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


In the src folder create a folder called META-INF and inside that create a file called persistence.xml.
Copy and paste the below content into the persistence.xml file.



xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
       http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

       <persistence-unit name="projectjpa">
           <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
              <class>com.User</class>

              <properties>
                     <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/test"/>
                     <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
                     <property name="openjpa.ConnectionUserName" value="root"/>
                     <property name="openjpa.ConnectionPassword" value="root"/>
                     <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
                    
                      <property name="openjpa.DynamicEnhancementAgent" value="false"/>
                     <property name="openjpa.RuntimeUnenhancedClasses" value="supported"/> 
              </properties>
       </persistence-unit>
</persistence>


7> Now open MySQL command prompt and create a database called test.
Mysql> create database test;
Mysql>use test;
Mysql>create table user (id int not null primary key, name varchar(200));
Mysql> insert into user vales(1,’ABC’);
Mysql> insert into user vales(2,’XYZ’);
8> Now right click on the project->Run As->Run on Server
9> You should be able to open the eclipse internal web browser and should be showed the result as below:
Query returned: 2