Home > Documentation > Developing > Tutorials > Developing Web applications > Accessing JDBC in Web applications |
This application will deal with populating an image on a database and later retrieving the same from the database. This tutorial will help you understand how to deal with data of type Blob. We will be using Derby database in this application. Please use images of smaller sizes to upload on to database. This is because there are some limitations associated with the derby database.
To run this tutorial, as a minimum you will be required to have installed the following prerequisite software:
Geronimo version 2.1.x, Java 1.5 runtime, and Eclipse Ganymede are used is used in this tutorial but other versions can be used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Europa)
Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:
create table pictures(name varchar(32) not null primary key, pic blob(16M));
index.jsp
.index.jsp
.
<%@ 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>Image Upload</title> </head> <body> <h2>Select a Image and Upload it</h2> <form action="/WebJDBC/ImageUpload"> <table> <tr> <td> Location of the Image(full path) </td> <td> <Input type="text" name="ImageLoc"> </td> </tr> <tr> <td> Name of the Image(Unique Name) </td> <td> <Input type="text" name="ImageName"> </td> </tr> <tr> <td> <Input type="submit" value="submit"> </td> <tr> </table> </form> </body> </html>
ImageUpload.java
.
package jdbc; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; public class ImageUpload extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { @Resource(name="jdbc/userds") private DataSource ds; static final long serialVersionUID = 1L; public ImageUpload() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doProcess(request, response); } protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection dbconnect = null; PreparedStatement stmnt = null; String pic=request.getParameter("ImageLoc"); String name=request.getParameter("ImageName"); try{ File f= new File(pic); FileInputStream fis=new FileInputStream(f); dbconnect= ds.getConnection(); stmnt = dbconnect.prepareStatement("INSERT INTO PICTURES (" + "NAME," + "PIC )" + " VALUES(?,?)"); stmnt.setString(1, name); stmnt.setBinaryStream(2, fis, (int)f.length()); stmnt.execute(); PrintWriter out= response.getWriter(); out.println("Congratulations your image has been successfully uploaded"); } catch(Exception e) { e.printStackTrace(); } finally { try{ dbconnect.close(); stmnt.close(); }catch(SQLException e){ e.printStackTrace(); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doProcess(request, response); } }
Once a request is submitted from index.jsp
for an image upload with the name and exact location of image the request is send to the ImageUpload servlet.
Once we have the image available a new File object is created from the image. Thereafter the image file is read as a binary stream. PreparedStatement is used to insert the image onto database. This completes the code for ImageUpload.
ImageDownload.jsp
.ImageDownload.jsp
:
<%@ 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>Download Image</title> </head> <body> <h2>Name a Image to download</h2> <form action="/WebJDBC/ImageDownload"> <table> <tr> <td> Name of the Image </td> <td> <Input type="text" name="ImageName"> </td> </tr> <tr> <td> <Input type="submit" value="submit"> </td> <tr> </table> </form> </body> </html>
ImageDownload.java
and add the following code:
package jdbc; import java.io.IOException; import java.io.OutputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; public class ImageDownload extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { @Resource(name = "jdbc/userds") private DataSource ds; static final long serialVersionUID = 1L; public ImageDownload() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doProcess(request, response); } protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection dbconnect = null; ResultSet rs = null; PreparedStatement stmnt = null; try { dbconnect = ds.getConnection(); String s=request.getParameter("ImageName"); stmnt = dbconnect.prepareStatement("SELECT PIC FROM PICTURES WHERE NAME=?"); stmnt.setString(1, s); rs = stmnt.executeQuery(); if (rs.next()) { // Get as a BLOB Blob aBlob = rs.getBlob(1); byte[] b = new byte[4096]; java.io.InputStream ip = aBlob.getBinaryStream(); OutputStream out = null; int c = 0; out = response.getOutputStream(); response.setContentType("image/jpeg"); while (c != -1) { c = ip.read(b); if (c > 0) { out.write(b, 0, c); out.flush(); } ip.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try{ dbconnect.close(); stmnt.close(); rs.close(); }catch(SQLException e){ e.printStackTrace(); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doProcess(request, response); } }
The ImageDownload servlet works in a similar way once the request is sent from ImageDownload.jsp
to it. The servlet uses
This completes the code for Image download.
The next step is to modify the geronimo-web.xml
deployment plan. We need to add the dependency element for the JDBC resource. Also we need to add a resource reference element for the userds datasource.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2"> <sys:environment> <sys:moduleId> <sys:groupId>default</sys:groupId> <sys:artifactId>WebJDBC</sys:artifactId> <sys:version>1.0</sys:version> <sys:type>car</sys:type> </sys:moduleId> <sys:dependencies> <sys:dependency> <sys:groupId>console.dbpool</sys:groupId> <sys:artifactId>jdbc_userds</sys:artifactId> </sys:dependency> </sys:dependencies> </sys:environment> <context-root>/WebJDBC</context-root> <nam:resource-ref> <nam:ref-name>jdbc/userds</nam:ref-name> <nam:pattern> <nam:groupId>console.dbpool</nam:groupId> <nam:artifactId>jdbc_userds</nam:artifactId> <nam:name>jdbc/userds</nam:name> </nam:pattern> </nam:resource-ref> </web-app>
Bookmark this on Delicious Digg this | Privacy Policy - Copyright © 2003-2013, The Apache Software Foundation, Licensed under ASL 2.0. |