0

My form1 looks like this,

<form method="post" id="myform" name="myform" action="upload" enctype="multipart/form-data">
    <div id="elements">  
    <ul id="ul">
        <li>Left File&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <input type="file" name="dataFile1" id="fileChooser1" /></li><li><br></li>
        <li>Right File&nbsp;&nbsp;&nbsp;&nbsp; : <input type="file" name="dataFile2" id="fileChooser2" /></li><li><br></li>
        <li>Runner.xlsx : <input type="file" name="dataFile3" id="fileChooser3" /></li><li><br></li>
        <li><button type="button" id="execute" onclick="ValidateFile()">Click to Upload files</button></li>
    </ul>
    </div>
</form>

I've declared a function like this to add hidden data to forms,

function addHidden(theForm, key, value) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = value;
theForm.appendChild(input);
}

in Validatefile()

var myDivText1 = ace.edit("editor").getValue();
var theForm = document.forms['myform'];
addHidden(theForm, 'mytxt1', myDivText1);
alert(myDivText1);
document.myform.submit();   

and in my upload.java servlet,

if (itemField.equals("dataFile1")) 
 {  //get the text of the editor here and save it

  String TEXT = request.getAttribute("mytxt1").toString();//this is being displayed as null
  System.out.println(TEXT);
  File uploadedFile = new File(fpath, fileName);
  item.write(uploadedFile);
  String f1 = "<span class='blue'>" + "Uploaded <b>left file</b> " +fileName+ "<br>" + "</span>";
  request.setAttribute("f1stat", f1);
}

while the alert in my Javascript function displays correct value, when I try to access this in my upload.java servlet it shows a null value.

I've used this function for another form too which is also it is passing the required data while this one fails, what is my mistake?

I've tried changing getAttribute to getParameter still getting Null value.

Code
  • 785
  • 3
  • 8
  • 19

3 Answers3

2

I think you should use request.getParameter("mytxt1") instead of request.getAttribute("mytxt1").

Priyesh
  • 2,041
  • 1
  • 17
  • 25
0
String TEXT = request.getAttribute("mytxt1").toString();   

Being displayed as null because you don't set any attribute named "mytxt1".

You should use

request.getParameter("mytxt1"); 

instead because you try to get something from a form with some name variables..

Code
  • 785
  • 3
  • 8
  • 19
canmurat
  • 159
  • 11
0

Because your enctype is "multipart/form-data", you can't get anything by using request.getParameter("mytxt1"), you can try apache fileupload component.

Tom
  • 2,857
  • 9
  • 46
  • 59
  • I am not trying to upload files, I am trying to pass data from jsp to java servlet using hidden in forms – Code Apr 10 '14 at 12:35
  • Then please remove multipart/form-data and file type input. If you have text input and file input,you should add extra code to handle them, or you can't get correct data at server side – Tom Apr 10 '14 at 12:46
  • See, I need multi part to upload files along whith that I want to pass some data – Code Apr 10 '14 at 12:47
  • That's why you should use something like apache fileupload component – Tom Apr 10 '14 at 12:55
  • I am using that to upload files, but I want to pass data also. – Code Apr 10 '14 at 12:56
  • See, there are two activities I wanna do 1. Upload files -> doing using apache commons 2. pass `
    ` text as hidden parameters to my servlet
    – Code Apr 10 '14 at 12:57
  • My another fomr **form2** as stated above is also a multipart thingy, but yet the hidden data is being passed – Code Apr 10 '14 at 12:58
  • 'FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = upload.parseRequest(request); Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) {// plain text } else { // file }' – Tom Apr 10 '14 at 13:37