Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code to retrieve the image from derby database

  1. Create a jsp page with the name ImageDownload.jsp.
  2. Append the jsp with the following code
    Code Block
    titleImageDownload.jsp
    borderStylesolid
    
    <%@ 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>
    
    As can be seen from <form action="/WebJDBC/ImageDownload"> this jsp requests ImageDownload servlet when the form is submitted.
  3. Create a new servlet ImageDownload.java and add the following code
    Code Block
    titleImageDownload.java
    borderStylesolid
    
    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);
    					out.write(b);
    					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);
    	}   	  	    
    }
    
    This completes the code for Image download.

Modifying deployment plan

Next step is to modify the deployment plan that is geronimo-web.xml. We need to add the dependency element for JDBC resource. Also we need to add a resource reference element for the userds datasource.

Code Block
titlegeronimo-web.xml
borderStylesolid

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1">
  <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%2Fuserds</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%2Fuserds</nam:artifactId>
          <nam:name>jdbc/userds</nam:name>
        </nam:pattern>
    </nam:resource-ref>
</web-app>

Deploy and Run