Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp File_upload Uploading Employee Profile with image

Related Tutorials


 
 

Share on Google+Share on Google+

Uploading Employee Profile with image

Advertisement
This tutorial will help you to understand how you can upload multiple files by using the Jsp. We should avoid to use Jsp for performing any logic, as Jsp is mainly used for the presentation logic, but at least we should know how we can use a java code ins

Uploading Employee Profile with image

     

This tutorial will help you to understand how you can upload multiple files by using the Jsp. We should avoid to use Jsp for performing any logic, as Jsp is mainly used for the presentation logic, but at least we should know how we can use a java code inside the jsp page. In Jsp the logic of the program is mainly written inside the scriptlet. In this example we are going to tell you how can make use of scriptlet to upload a image in the employee file.

In this example we are going to tell you how we can upload image in a file by using Jsp and how they will get stored on the particular memory area. To get the desired result firstly we need to need to make a jsp page which will work as a controller and where we need to import some packages, classes and interfaces. For this program we need many classes and interfaces. 

The classes and interfaces we have used in the program are List and Iterator of java.util.* package, ServletFileLoad class of package org.apache.commons.fileupload.servlet.*, DiskFileItemFactory class of package org.apache.commons.fileupload.disk.DiskFileItemFactory and package org.apache.commons.fileupload.*. These are the classes which have been provided to us by the apache to help us in file uploading.

Inside the scriptlet call the isMultipartContent() method of class ServletFileUpload which takes one parameter request. This method checks whether there is a file upload request or not. It returns the boolean value. If there is a request for uploading the file then it makes a object of DiskFileItemFactory class and implements FileItemFactory. The implementation of FileItemFactory means that it creates FileItem instances which keep their content either in memory or disk. While using the DiskFileItemFactory the temporary files are automatically deleted as soon as they are no longer needed. The interface FileItemFactory is a factory for creating FileItem instances. Previously we have used one word FileItem, this interface represents a file or form item that was received within a multipart/form-data POST request.  Now pass the object of FileItemFactory which has the reference of class DiskFileItemFactory inside the constructor of class ServletFileUpload(). This is used to constructs an instance of this class which uses the supplied factory to create FileItem instances. Now declare a variable of type List with the initial value null. Use the method parseRequest() and pass a request object as its parameter which returns a list of FileItem instances parsed from the request, in the order they were transmitted. This method will throw FileUploadException if there is any problem while reading or parsing the request. Don't worry this is not going to happen in this example. To iterate the list of FileItem instances parsed from the request use a method iterator() of interface List which returns an iterator over the elements in this list in proper sequence. To get the list of FileItem instances we need to make use of while loop This loop will continue until the end of FileItem doesn't reached. 

Now make another jsp page which will be used for the presentation. In this Jsp page we will have one action which tells where the request will be forwarded when there is any request for file uploading. The request will be sent in the form of post method, In post method the parameters are send within a message body. As we have to upload multiple files so we need enctype. This is used to determine how the data is encoded. Whenever data is transmitted from one place to another, there should be some agreement between the party how the data will be represented. 

In our example we are making employee form so we need to make a form according to the employee table. In this jsp page there will be one submit button and one reset button.  

This is a very a simple program and can be used for maintaining the employee records along with his or her picture. 

Complete code of the employee_upload_profile_image.html file:

<html>
 <head><title>Upload page</title></head></p> <p><body>
 <form action="employee_upload_profile_image.jsp" method="post" enctype
="multipart/form-data" name="form1" id="form1">
   <center>
   <table border="2">
       <tr>
	       <td align="right"><b>Employee Name:</td>
		   <td ><input type="text" name="emp_name"></td>
	   </tr>
	   <tr>
	       <td align="right"><b>Employee Address:</td>
		   <td ><input type="text" name="address1"></td>
		   	   </tr>
	   <tr>
	       <td>
		   </td>
		   <td>
		      <input type="text" name="address2">
		   </td>
	   </tr>
	   <tr>
	       <td align="right"><b>Contact Number:</td>
		   <td ><input type="text" name="contact_number"></td>
	   </tr>
       <tr>
	       <td align="right"><b>Employee Email ID:</td>
		   <td ><input type="text" name="email_id"></td>
	   </tr>
	          <tr>
	       <td align="right"><b>Employee Image </td>
	       <td>
		       <input name="file" type="file" id="file">
		   <td>
	   </tr>
	   	 <tr>
		    <td align="center">
               <input type="submit" name="Submit" value="Submit"/>
			   <input type="reset" name="Reset" value="Reset"/>
      			</td>
		 </tr>
    </table>
	</center>
 </form>
 </body>
 </html>

Output for the above file:

Code of the employee_upload_profile_image.jsp file:

<%@ page import="java.util.List" %>
   <%@ page import="java.util.Iterator" %>
   <%@ page import="java.io.File" %>
   <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
   <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
   <%@ page import="org.apache.commons.fileupload.*"%>
   <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <center><h1>Your  Profile has been Uploaded</h1></center>
   <%!
     String emp_name="";	 
	 String emp_c_number="";
	 String emp_emailid="";
     String address1="";
	 String address2="";
	 int count1=0,count2=0,count3=0,count4=0,count5=0;
 %>
 <%
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 if (!isMultipart) {
 } else {
   FileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload upload = new ServletFileUpload(factory);
   List items = null;
   try {
   items = upload.parseRequest(request);
   } catch (FileUploadException e) {
   e.printStackTrace();
   }
   Iterator itr = items.iterator();
   while (itr.hasNext()) 
	   {
   FileItem item = (FileItem) itr.next();
   if (item.isFormField())
	   {
	      String name = item.getFieldName();
		  String value = item.getString();
		  if(name.equals("emp_name"))
	           {
			   emp_name=value;
            		 count1=1;
			   }
			  if(name.equals("address1"))
	                  {  
				         address1=value;            		 
                         count2=2;
					  }
			  if(name.equals("address2"))
	                  {  
				         address2=value;            		 
                         count5=5;
					  }
			  if(name.equals("contact_number"))
	                  {
			         emp_c_number=value;
			         count3=3;
					  }
            					  if(name.equals("email_id"))
	             {
			      count4=4;
				  emp_emailid=value;
				 }	    
		 		 
		       } else
	   {
    try {
	   String itemName = item.getName();
   File savedFile = new File(config.getServletContext().getRealPath("/")
+"emp_image\\image\\"+itemName);
   item.write(savedFile);
     %><center></table><table ><tr><td width="210"></td><td> <img  border="2"
 src=image/<%=itemName %> width="137"  height="140"></td></tr></table><table
 border="2" width="350">
      <% if(count1==1)
      		 out.println("<tr><td align='left'><b>Name:</td><td><b>"+emp_name);
	  if(count2==2)	 
		     out.println("</td><tr><td align='left'><b>Addresss:</td>
<td><b>"+address1);
	  if(count5==5)	 
		     out.println("</td><tr><td align='left'><b></td><td><b>"+
address2);
	  if(count3==3)
		     out.println("</td><tr><td align='left'><b>Contact No</td>
<td><b>"+emp_c_number);
	  if(count4==4)
		     out.println("</td><tr><td align='left'><b>Email ID
</td><td><b>"+emp_emailid);
   } catch (Exception e) {
   e.printStackTrace();
   }
   }
   }
   }
   %>
     </td></tr></table></center>

Output for the above file:

Download Files.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 25, 2008

Related Tutorials

Discuss: Uploading Employee Profile with image   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:11
surya
August 15, 2011
Ecxeption on DeferredFileOutputStream

i got an error in the jsp page such as no ClassDefFoundError DeferredFileOutputStream. would u please suggest me how can i rectify this one plzzzzzzzzzzzzzzzzzzzzz
jaliya
October 23, 2011
Dev

thank you vary much
Janaki
December 9, 2011
I want the Employee Profile

I want the Employee Profile and experiences letter format
Amber
January 16, 2012
CQdswgRhMOAWD

That's a smart asnwer to a difficult question.
vishnu
March 9, 2012
jdbc program for connecting basic employee table

Dear Sir, I want a JDBC program which will connect a basic employee table of oracle 11g and display it to the console. I want all the details through which i can display the output. I am having editplus and microsoft access to proceed. Please tell all the details which gives me the required result.
Manpreet Kaur
March 24, 2012
output related query

Hi, I just want to know that why employee_upload_profile_image.jsp file's output is not showing value of various fields?
Charlie
July 10, 2012
File upload

Here in this code you have mentioned that <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%> <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%> <%@ page import="org.apache.commons.fileupload.*"%> Code is giving me invalid import error .. what to do ?
Gabriel
October 12, 2012
Upload file to database.

I don't see in your code where you make the database connection. Also, I believe the information you are providing is incomplete. Here is the error I get when I execute your code: org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 17 in the generated java file Only a type can be imported. org.apache.commons.fileupload.servlet.ServletFileUpload resolves to a package An error occurred at line: 18 in the generated java file Only a type can be imported. org.apache.commons.fileupload.disk.DiskFileItemFactory resolves to a package An error occurred at line: 76 in the jsp file: /employee_upload_profile_image.jsp String literal is not properly closed by a double-quote 73: <% if(count1==1) 74: out.println("<tr><td align='left'><b>Name:</td><td><b>"+emp_name); 75: if(count2==2) 76: out.println("</td><tr><td align='left'><b>Addresss:</td> 77: <td><b>"+address1); 78: if(count5==5) 79: out.println("</td><tr><td align='left'><b></td><td><b>"+
Vishnu Priya
January 15, 2014
Error with this code

Iam using netbeans 6.8, Tomcat 6 . The following error is shown while debugging this code. Kindly help Compiling 1 source file to C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\classes C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:9: error: package org.apache.commons.fileupload.servlet does not exist import org.apache.commons.fileupload.servlet.ServletFileUpload; C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:10: error: package org.apache.commons.fileupload.disk does not exist import org.apache.commons.fileupload.disk.DiskFileItemFactory; C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:11: error: package org.apache.commons.fileupload does not exist import org.apache.commons.fileupload.*; C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:81: error: cannot find symbol boolean isMultipart = ServletFileUpload.isMultipartContent(request); ^ symbol: variable ServletFileUpload location: class newjsp_jsp C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:84: error: cannot find symbol FileItemFactory factory = new DiskFileItemFactory(); ^ symbol: class FileItemFactory location: class newjsp_jsp C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:84: error: cannot find symbol FileItemFactory factory = new DiskFileItemFactory(); ^ symbol: class DiskFileItemFactory location: class newjsp_jsp C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:85: error: cannot find symbol ServletFileUpload upload = new ServletFileUpload(factory); ^ symbol: class ServletFileUpload location: class newjsp_jsp C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:85: error: cannot find symbol ServletFileUpload upload = new ServletFileUpload(factory); ^ symbol: class ServletFileUpload location: class newjsp_jsp C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:89: error: cannot find symbol } catch (FileUploadException e) { ^ symbol: class FileUploadException location: class newjsp_jsp C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:95: error: cannot find symbol FileItem item = (FileItem) itr.next(); ^ symbol: class FileItem location: class newjsp_jsp C:\Users\keltron022\Documents\NetBeansProjects\amc3\build\generated\src\org\apache\jsp\newjsp_jsp.java:95: error: cannot find symbol FileItem item = (FileItem) itr.next(); ^ symbol: class FileItem location: class newjsp_jsp 11 errors C:\Users\keltron022\Documents\NetBeansProjects\amc3\nbproject\build-impl.xml:481: The following error occurred while executing this line: C:\Users\keltron022\Documents\NetBeansProjects\amc3\nbproject\build-impl.xml:229: Compile failed; see the compiler error output for details. BUILD FAILED (total time: 0 seconds)
DMCA.com