我使用以下Javascript
功能将invisible
数据添加到表单中,
function addHidden(theForm, key, value) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;'name-as-seen-at-the-server';
input.value = value;
theForm.appendChild(input);
}
我有两种形式如下
Form1中
<form method="get" id="execform" name="toexecute" enctype="multipart/form-data">
<div id="to execute">
<ul id=ul>
<li><button type= "button" id="execute1" onclick="saveTextAsFile()">Click to execute</button></li><li><br></li>
</ul>
</div>
</form>
saveTextAsFile()
var script = ace.edit("editor");
var myDivText = script.getValue();
var theForm = document.forms['toexecute'];
addHidden(theForm, 'mytxt', myDivText);
document.toexecute.submit();
窗口2
<form method="post" id="theform" name="myform" action="upload" enctype="multipart/form-data">
<div id="elements">
<ul id="ul">
<li>Left File : <input type="file" name="dataFile1" id="fileChooser1" /></li><li><br></li>
<li>Right File : <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>
ValidateFile()
var myDivText1 = ace.edit("editor").getValue();
var theForm1 = document.forms['myform'];
addHidden(theForm1, 'mytxt1', myDivText1);
alert(myDivText1);
document.myform.submit();
表单1 的隐藏数据正在成功添加,传递,我可以在我的servlet中访问它,而我在表单2中使用了相同的方法/ strong>我的servlet正在抛出nullpointerexception
。我的错是什么?
在我的servlet中提交 form2 时,
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{//activated by the form named myform of line 93 in geco.jsp
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
String fpath= null;
if (cookies != null) {//Get cookie initilised when user uploads geco script
for (Cookie cookie : cookies) {
if (cookie.getName().equals("thecookie")) {
fpath = cookie.getValue();
}
}
}
response.setContentType("text/html");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try
{
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext())
{
FileItem item = (FileItem) iterator.next();
if (!item.isFormField())
{
String fileName = item.getName();
String itemField = item.getFieldName();
if (itemField.equals("dataFile1"))
{ //get the text of the editor here and save it
String TEXT = request.getAttribute("mytxt1").toString();//This is where it is displaying null
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);
}
if (itemField.equals("dataFile2"))
{
File uploadedFile = new File(fpath, fileName);
item.write(uploadedFile);
String f2 = "<span class='blue'>" +"Uploaded <b>right file</b> " +fileName+ "<br>" + "</span>";
request.setAttribute("f2stat", f2);
}
if (itemField.equals("dataFile3"))
{
File uploadedFile = new File(fpath, fileName);
item.write(uploadedFile);
File uploadedFile1 = new File(fpath, "Runner.xlsx");
item.write(uploadedFile1);
String f3 = "<span class='blue'>" +"Uploaded <b>Config file</b> " +fileName+ "<br>" + "</span>";
request.setAttribute("f3stat", f3);
String content="";
if(new File(fpath).exists())
{
String my = fpath + "/gecofile.geco";
BufferedReader reader = new BufferedReader(new FileReader(my));
String line1;
while ((line1 = reader.readLine()) != null)
{
content= content + line1 +"\n";
}
}
request.setAttribute("File_Text", content);
RequestDispatcher rd = request.getRequestDispatcher("geco");
rd.forward(request, response);
}
PS:当 form1 提交给doGet()并且在doPost()中时,我设置了cookie。我正在访问相同的cookie值。
PS:我已尝试将getAttribute
更改为getParameter
,但该值仍为空。
答案 0 :(得分:0)
输入字段正确添加。如果来自隐藏输入的数据存在,请使用 FireBug 检查请求,应该是。这是你的servlet问题。也许是因为你为表格使用不同的名称。也许是因为你为input.name
提供了不同的值。上面的代码工作正常。原因可能还在于您的第一个表单toexecute
没有操作字段。
您向我们提供了有关服务器端代码的小数据,以解决此问题。
编辑:
我没有Java Servlets的经验,所以我只是在拍摄。第一次发送 GET 和request.getAttribute()
时。第二次发送 POST ,并且存在问题。由于您发送的图像是编码数据,因此无法正常读取。我想你会在这里找到答案:
HttpServletRequest get JSON POST data
EDIT2:
试试这个方法。它应该做的工作:
request.getServletContext().getAttribute("attr_name");
EDIT3:
由于您使用的是 Tomcat 6 ,因此您遇到了问题multipart/form-data
。这是类似的问题和解决方案:
Convenient way to parse incoming multipart/form-data parameters in a Servlet
您应该转到 Tomcat 7 。
答案 1 :(得分:0)
在您的servlet中,您需要获取名为mytxt1
的请求参数而不是具有该名称的请求中设置的属性(除非您没有向我们展示某些代码)。改变这一行:
String TEXT = request.getAttribute("mytxt1").toString();//This is where it is displaying null
到
String TEXT = request.getParameter("mytxt1");