Results 1 to 1 of 1
  1. #1
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Default Fileupload String null help

    well,im working on making a profile of employee..there i need to enter all the other details of employee along with uploading his profile image...this is my code...

    Java Code:
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
           
        </head>
        <body style="overflow-y:scroll;overflow:scroll;overflow-x: hidden;">
          <%p=(String)request.getParameter("p");%> <!-- This is,..im getting the string from previous.jsp where i gave p number..,and no problem in it..-->
    
    
          <form name="ad" action="profile.jsp" enctype="multipart/form-data" method="post">
        
        <fieldset style="border:none;">
     <legend align="center">BIO-DATA</legend>
          <table>
            <tr><td>P Number:</td> <td><input type="text"  readonly="readonly" name="p" value="<%=pfnum%>"  /></td><td>SR Number:</td> <td><input type="text" name="sr"  /></td></tr>
            <tr><td>Emp.name:</td> <td><input type="text"  name="nm" /></td><td>&nbsp;&nbsp;&nbsp;</td><td><input type="file" name="ImageFile"  id="ImageFile" /></td></tr>
            <tr><td>Emp.Old Name:</td> <td><input type="text"   name="onm" /></td><td>Religion:</td> <td><input type="text"   name="rg" /></td></tr>
            <tr><td>Father/Husband Name:</td> <td><input type="text"   name="fnm" /></td><td>DOB:</td> <td><input type="text"   name="dob"/></td></tr>
            <tr><td>Address(present):</td> <td><input type="text"   name="ad" /></td><td>Addn.Education:</td> <td><input type="text"  name="edu" /></td></tr>
            <tr><td>Address(permanent):</td> <td><input type="text"   name="pad" /></td><td>Medical Code:</td> <td><input type="text"   name="med" /></td></tr>
           <tr><td>Status: </td><td><select name="stt" style="width: 150px"><option>SC</option><option>ST</option><option>BC</option><option>OC</option><option>OBC</option></select><br /></td></tr>
          <tr><td>Community:</td> <td><input type="text"   name="cm" /></td></tr>
          <tr><td>Gender: </td><td><select name="gn"style="width: 150px"><option>Male</option><option>Female</option></select><br /></td></tr>
         <tr><td>Education: </td><td><select name="ed" style="width: 150px"><option>BCOM</option><option>BSC</option></select><br /></td></tr>
         <tr><td>Recruitement Code: </td><td><select name="rc" style="width: 150px"><option>CG</option><option>MN</option></select><br /></td></tr>
           <td><input type="submit" name="submit" value="submit"  /></td>
    
    
          </table>
    </fieldset>
    </form>
    
    </body>
    </html>
    then it passes to profile.jsp
    Java Code:
    <%@ page import="java.sql.*"%>
    <%@ page import="java.io.File"%>
     <%@ page import="org.apache.commons.fileupload.*"%>
     <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
     <%@ page import="org.apache.commons.io.*"%>
     <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
     <%@ page import="org.apache.commons.fileupload.FileUploadException"%>
      <%@ page import="java.util.List"%>
     <%@ page import="java.util.Iterator"%>
    <%@ include file="add.jsp"%>
    <%!
    String nm="";
    String onm="";
    String fnm="";
    String ad="";
    String pad="";
    String stt="";
    String cm="";
    String gn="";
    String ed="";
    String rc="";
    String sr="";
    String rg="";
    String dob="";
    String edu="";
    String med="";
    
    String ImageFile="";
    String itemName = "";
    %>
    
     <%
    try
    {
    Connection con;
    //Statement st;
    /*String pfnum="";
    p=request.getParameter("p");
    String nm=request.getParameter("nm");
    String onm=request.getParameter("onm");
    String fnm=request.getParameter("fnm");
    String ad=request.getParameter("ad");
    String pad=request.getParameter("pad");
    String stt=request.getParameter("stt");
    String cm=request.getParameter("cm");
    String gn=request.getParameter("gn");
    String ed=request.getParameter("ed");
    String rc=request.getParameter("rc");
    String sr=request.getParameter("sr");
    String rg=request.getParameter("rg");
    String dob=request.getParameter("dob");
    String edu=request.getParameter("edu");
    String med=request.getParameter("med");*/
    
    
    
    PreparedStatement st;
    
    int count1=0,count2=0,count3=0,count4=0,count5=0,count6=0,count7=0,count8=0,count9=0,count10=0, count11=0,count12=0,count13=0,count14=0,count15=0,count16=0;
    
       // String pfnum=request.getParameter("pfnum");
        Class.forName("oracle.jdbc.driver.OracleDriver");
    
        con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ds","ds");
        //st=con.prepareStatement("insert into profile(p,nm,onm,fnm,ad,pd,st,cm,gn,ed,rc,sc,image,rg,dob,edu,med) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
         //st=con.createStatement();
    
    /*st.setString(1,p);
    st.setString(2,nm);
    st.setString(3,onm);
    st.setString(4,fnm);
    st.setString(5,ad);
    st.setString(6,pad);
    st.setString(7,stt);
    st.setString(8,cm);
    st.setString(9,gn);
    st.setString(10,ed);
    st.setString(11,rc);
    st.setString(12,sr);
    st.setString(13,itemName);
    st.setString(14,rg);
    st.setString(15,dob);
    st.setString(16,edu);
    st.setString(17,med);*/
    
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (!isMultipart)
        {
    out.print("Failed");
    }
    else
    {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = null;
    //try
    //{
    items = upload.parseRequest(request);
    
    
    //}
    //catch (FileUploadException e)
    //{
    //e.getMessage();
    //}
    
    
    
    Iterator itr = items.iterator();
    while (itr.hasNext())
    {
    FileItem item = (FileItem) itr.next();
    if (item.isFormField())
    {
    String name = item.getFieldName();
    String value = item.getString();
    //session.setAttribute(name, value);
    if(name.equals("ImageFile"))
    {
    ImageFile=value;
    }
    if(name.equals("p"))
    {
    p=value;
    count1=1;
    }
    if(name.equals("nm"))
    {
    nm=value;
    count2=2;
    }
    if(name.equals("onm"))
    {
    onm=value;
    count3=3;
    }
    if(name.equals("fnm"))
    {
    fnm=value;
    count4=4;
    }
    if(name.equals("ad"))
    {
    ad=value;
    count5=5;
    }
    if(name.equals("pad"))
    {
    pad=value;
    count6=6;
    }
    if(name.equals("stt"))
    {
    stt=value;
    count7=7;
    }
    if(name.equals("cm"))
    {
    cm=value;
    count8=8;
    }
    if(name.equals("gn"))
    {
    gn=value;
    count9=9;
    }
    if(name.equals("ed"))
    {
    ed=value;
    count10=10;
    }
    if(name.equals("rc"))
    {
    rc=value;
    count11=11;
    }
    if(name.equals("sr"))
    {
    sr=value;
    count12=12;
    }
    if(name.equals("rg"))
    {
    rg=value;
    count13=13;
    }
    if(name.equals("dob"))
    {
    dob=value;
    count14=14;
    }
    if(name.equals("edu"))
    {
    edu=value;
    count15=15;
    }
    if(name.equals("med"))
    {
    med=value;
    count16=16;
    }
    if(med==null);
    {
    out.print("This means..,the fields are NULL");}
    
    }
    else
    {
    //try
    //{
    itemName = item.getName();
    File savedFile = new File(config.getServletContext().getRealPath("/")+"uploadim\\"+itemName);
    item.write(savedFile);
       
        
    out.println("<tr><td><b>Your file has been saved at the loaction:</b></td></tr><tr><td><b>"+config.getServletContext().getRealPath
    ("/")+"uploadedFiles"+"\\"+itemName+"</td></tr>");
    
    //}
    //catch (Exception e)
    //{
    //out.println("Error"+e.getMessage());
    //}
    }
    }
    //try
    //{
     
    //if(count1==1 && count2==2 && count3==3 && count4==4 && count5==5 && count6==6 && count7==7 && count8==8 && count9==9 && count10==10 && count11==11 && count12==12 && count13==13 && count14==14 && count15==15 && count16==16)
    //st.executeUpdate("insert into profile(p,nm,onm,fnm,ad,pd,st,cm,gn,ed,rc,sc,image,rg,dob,edu,med) values('"+p+"','"+nm+"','"+onm+"','"+fnm+"','"+ad+"','"+pad+"','"+stt+"','"+cm+"','"+gn+"','"+ed+"','"+rc+"','"+sr+"','"+itemName+"','"+rg+"','"+dob+"','"+edu+"','"+med+"')");
    st=con.prepareStatement("insert into profile(pfnum,nm,onm,fnm,ad,pd,st,cm,gn,ed,rc,sc,image,rg,dob,edu,med) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    st.setString(1,p);
    st.setString(2,nm);
    st.setString(3,onm);
    st.setString(4,fnm);
    st.setString(5,ad);
    st.setString(6,pad);
    st.setString(7,stt);
    st.setString(8,cm);
    st.setString(9,gn);
    st.setString(10,ed);
    st.setString(11,rc);
    st.setString(12,sr);
    st.setString(13,itemName);
    st.setString(14,rg);
    st.setString(15,dob);
    st.setString(16,edu);
    st.setString(17,med);
    //st.executeUpdate("insert into sample(image) values ('"+itemName+"')");
    //st.executeUpdate();
    int s=st.executeUpdate();
    if(s>0) {
    out.println("Uploaded successfully !");
    }
    else{
    out.println("unsucessfull to upload file.");
    }
    //}
    //catch(Exception el)
    //{
    //out.println("Inserting error"+el.getMessage());
    //}
    }
    }
    catch (Exception e){
    out.println(e.getMessage());
    }
    
     %>
    Jarfiles:-

    1)commons-io-1.4.jar
    2)commons-fileupload-1.1.2.jar
    3)cos.jar

    Here in this code,the image is getting uploaded in folder "uploadim",but all the other fields are getting into null value.., and i know that in multipart we cannot use request.getParameter() and so i havent used it.., but still im getting null value for all the text type of data..

    Please help me....its very urgent..,and moreover please help me by providing with code that i have to use in it,..as im new to jsp stuff...
    Last edited by danpotter; 01-10-2013 at 08:54 PM.

Similar Threads

  1. Replies: 0
    Last Post: 03-16-2012, 10:37 PM
  2. Replies: 0
    Last Post: 02-04-2011, 05:05 PM
  3. Replies: 1
    Last Post: 05-19-2010, 09:10 AM
  4. FileUpload Error
    By sandeep.ctg in forum Web Frameworks
    Replies: 0
    Last Post: 04-07-2010, 01:06 PM
  5. String returning null value
    By impact in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 07:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •