0

I am trying to store a file path in jsp along with some values in database from . The problem is that when i try to access a form value (which is a select option and text input) , and i am trying to display the value it shows null.

here is my jsp file of the form ,here dynamic dropdowns and ajax is working perfectly.

<form method="post" action="uploadfile.jsp" enctype="multipart/form-data" >
<div id="Layer1" style="position:absolute;text-align:left;left:373px;top:138px;width:608px;height:497px;z-index:17;">

<div id="wb_Text2" style="position:absolute;left:47px;top:35px;width:101px;height:18px;z-index:0;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">Department</span></div>
<div id="wb_Text3" style="position:absolute;left:47px;top:103px;width:81px;height:34px;z-index:1;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">Couse<br></span></div>
<div id="wb_Text1" style="position:absolute;left:48px;top:177px;width:98px;height:18px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">Sem</span></div>
<div id="wb_Text4" style="position:absolute;left:49px;top:252px;width:77px;height:18px;z-index:3;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">Subject</span></div>
<div id="wb_Text5" style="position:absolute;left:49px;top:327px;width:76px;height:18px;z-index:4;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">File Type</span></div>
<input type="submit" id="Button2" name="" value="UPLOAD" style="position:absolute;left:353px;top:400px;width:116px;height:48px;z-index:5;">
<input type="submit" id="Button1" name="" value="VIEW" style="position:absolute;left:189px;top:400px;width:124px;height:48px;z-index:6;">
<select name="Combobox1" size="1" id="Combobox1" style="position:absolute;left:189px;top:313px;width:292px;height:32px;z-index:7;">
<option selected value="select">--Select--</option>
<option value="Pdf">df</option>
<option value="ppt">ppt</option>
<option value="doc">doc</option>
<option value="others">others</option>
</select>
<select name="dept" size="1" id="Combobox2" onchange="getId(this.value);" style="position:absolute;left:189px;top:35px;width:292px;height:32px;z-index:8;">

<option selected value="select" selected>--Select--</option>
<% 


         Connection conn=null;
  // String fname="select file_path from file1";
   String driverName ="oracle.jdbc.driver.OracleDriver";
         Class.forName(driverName);
         String serverName = "abcd";
         String serverPort ="2222";
         String sid= "XE";
         String url="jdbc:oracle:thin:@" +serverName+":"+serverPort+":"+sid;
         String username ="system";
         String password ="lokmnnd";
         conn= DriverManager.getConnection(url,username,password);
   Statement st=conn.createStatement();

   // ResultSet s=st.executeQuery("Select SUBSTR(file_path,INSTR(file_path,'\',-1,1)+1) as fname from file1");
  // ResultSet rt=st.executeQuery("select fname from s");
    ResultSet rs=st.executeQuery("Select * from dept"); 



while(rs.next())
{

    String id = rs.getString("did");
String fname = rs.getString("dname"); 


%>
<option value="<%=id %>"><%=fname %></option>
<%
}
%>
</select>




<select name="course" size="1" id="course" onchange="getIdq(this.value);" style="position:absolute;left:189px;top:105px;width:292px;height:32px;z-index:9;">
<option selected value="--Select">--Select--</option>
</select>
<select name="Combobox1" size="1" id="semester" onchange="getIde(this.value);"style="position:absolute;left:189px;top:177px;width:292px;height:32px;z-index:10;">
<option selected value="--Select--">--Select--</option>
</select>
<select name="Combobox1" size="1" id="subject" style="position:absolute;left:189px;top:245px;width:292px;height:32px;z-index:11;">
<option selected value="--Select--">--Select--</option>
</select>
<input type="file" name="photo"  style="position:absolute;left:189px;top:245px;width:292px;height:28px;z-index:11;"/>
<input type="text" name="filedesc"  style="position:absolute;left:189px;top:260px;width:292px;height:28px;z-index:11;"/>
</div>
</form>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function getId(val)
{

        $.ajax({
        type:"POST",
        url:"courseselect.jsp",
        data:"uno="+val,
        success:function(data){
            $("#course").html(data);
        }
    });
}

function getIdq(val)
{

        $.ajax({
        type:"POST",
        url:"semesterselect.jsp",
        data:"uno="+val,
        success:function(data){
            $("#semester").html(data);
        }
    });
}

function getIde(val)
{

        $.ajax({
        type:"POST",
        url:"subjectselect.jsp",
        data:"uno="+val,
        success:function(data){
            $("#subject").html(data);
        }
    });
}
</script>

Now, when i am trying to store the values in the database. the value of the form fields are retrieved as null. here is my uploadfile.jsp

<%@ page import="java.io.*,java.sql.*,java.util.zip.*,java.lang.*" %>

<%
String saveFile="";
String contentType = request.getContentType();

if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("C:/UploadedFiles/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
//fileOut.flush();
//fileOut.close();
}
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<%out.println(saveFile);%></td></tr></table>

<%
Connection connection = null;

PreparedStatement psmnt = null;

       // out.println("a");
       String a =request.getParameter("dept");
       out.println(a);
       // int no = Integer.parseInt(request.getParameter("course"));
        //out.println(no);

        //String b = request.getParameter("course");
      String c= request.getParameter("sem");
      String d= request.getParameter("subject");
      String e= request.getParameter("filetype");
      out.println('e');
      String f=request.getParameter("filedesc");
      out.print(f);

String driverName ="oracle.jdbc.driver.OracleDriver";
         Class.forName(driverName);
         String serverName = "abcd";
         String serverPort ="2222";
         String sid= "XE";
         String url="jdbc:oracle:thin:@" +serverName+":"+serverPort+":"+sid;
         String username ="system";
         String password ="lokmnnd";
         connection= DriverManager.getConnection(url,username,password);
psmnt = connection.prepareStatement("insert into ffile(department,course,sem,subject,filetype,fildes,filepath) values(?,?,?,?,?,?,?)");
psmnt.setString(7, ff.getPath());
psmnt.setString(1, a);
psmnt.setInt(2, no);
psmnt.setString(3, c);
psmnt.setString(4, d);
psmnt.setString(5, e);
psmnt.setString(6, f);


int s = psmnt.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");
}
else{
System.out.println("Error!");
}

%>

After executing uploadfile, the file path is uploaded perfectly, but the values retrieved from the form , i.e. , the variables a and f are displaying null. I am not able to figure out the problem, please help me solve this issue.

roeygol
  • 4,908
  • 9
  • 51
  • 88
xyz
  • 13
  • 3
  • While using `enctype="multipart/form-data"` you have fetch other input field values in the same way as you have extracted `file`. Sadly `request.getParameter()` won't work here. –  Oct 23 '16 at 11:49
  • Can you tell me the changes that i have to do to fetch other values? – xyz Oct 23 '16 at 14:21
  • You have to follow same steps for others, as you did for file upload –  Oct 23 '16 at 16:20

0 Answers0