java - 将隐藏数据添加到表单时出错

标签 java javascript servlets

我的form1看起来像这样,

<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>

我已经声明了一个这样的函数来添加隐藏数据到表单,

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

Validatefile()

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

在我的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);
}

虽然我的 Javascript 函数中的警报显示正确的值,但当我尝试在 upload.java servlet 中访问它时,它显示空值。

我也将此函数用于另一种表单,它也传递了所需的数据,而此表单失败了,我的错误是什么?

我尝试更改 getAttributegetParameter仍然得到 Null 值。

最佳答案

我认为您应该使用request.getParameter("mytxt1")而不是request.getAttribute("mytxt1")

关于java - 将隐藏数据添加到表单时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22987560/

相关文章:

java - 无法获取从jsp到servlet的传递值

java.lang.ClassNotFoundException : javax. servlet.Servlet

java - 尝试使用 servlet 和 jsp 插入 mysql 时出现错误

javascript - 在图像上的新选项卡中打开链接,单击 Angular 而不使用弹出窗口拦截器

javascript - 带有自定义服务器端的 bootstrap formvalidation.io 远程验证器返回错误消息

java - 如何模拟 getApplicationContext

java - 具有可变列数的 Wicket 表

javascript - react 中列表未更新

java - String.split更多正则表达式字符

java - 创建第一个应用程序时 Flutter 出现错误 :checkDebugDuplicateClasses