I've the following Javascript
function to add invisible
data into my form,
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);
}
and I have two forms as follows
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();
form2
<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();
The hidden data of form 1 is being successfully added, passed and I am able to access that in my servlet, while I've used the same method in form 2 my servlet is throwing a nullpointerexception
. What is my mistake?
When form2 is being submitted in my servlet,
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: The cookie is set when form1 is submitted to doGet() and here in doPost() I am accessing the same cookie value.
PS: I've tried changing getAttribute
to getParameter
still the value remains null.