Versions Compared

Key

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

...

  1. Right click on WebContent and Select New->jsp.





  2. Name the jsp as index.jsp and Select Next.





  3. Select Finish. This will create a template for index.jsp.





  4. Add the following code to index.jsp.
    Code Block
    titleindex.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>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>
    
    The <form action="/WebJDBC/ImageUpload"> suggests that once the form is submitted the request will be passed to ImageUpload. Next step is to add the servlet ImageUpload to process the request sent by the JSP client.
  5. Right click on Java Resources and Select New->other.





  6. Select Web->Servlet. Select Next.





  7. Name the java package as jdbc and class as ImageUpload. Select Next->Next.





  8. Select Finish.


  9. Add the following code to ImageUpload.java.
    Code Block
    titleImageUpload.java
    borderStylesolid
    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);
    	}   	  	    
    }
    

...

Code Block
titlegeronimo-web.xml
borderStylesolid
<?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%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

  1. Start the server within Eclipse.
  2. Right click on WebJDBC project and select run on server.
  3. Once done the application will be deployed on the server.
  4. Launch the application with the following link http://localhost:8080/WebJDBC/





  5. Fill up the form with the a image location and name. The same name will be used while populating the database with the image data. Select Submit once done.





  6. If your image is successfully inserted into the database you will get a Congratulation message.





  7. To retrieve an image from the database launch the following portlet http://localhost:8080/WebJDBC/ImageDownload.jsp. Select submit once done.





  8. This will display the image in the browser