0

I am maintaining a console log on my web page to display errors/exceptions and success cases as shown below

enter image description here

So once the user selects valid files and uploads them, a servlet uploads files and returns uploaded path to the console like this

enter image description here

As you can see in my second image the console is also refreshed loosing all the previous messages, I don't want this to happen. How do I do this?

I am populating the div tag with holds the console as follows from JS

else if(FileName1 == FileName3 || FileName1 == FileName4 || FileName2 == FileName3 || FileName2 == FileName4)
{
    var err1 = document.getElementById("box");
    err1.innerHTML = "Configuration file and Geco script should not be the same as left or right files. Please check your uploads";
    err1.style.color = "Red";
}
//else if(FileName1.value)
else
{
    var scc1 = document.getElementById("box");
    scc1.innerHTML = "Uploading files, the page might refresh";
    scc1.style.color = "Blue";
    document.myform.submit();   
}

the the DIV tag which holds console gets values from servlet as follows

<div id="box">${f1stat}<br>${f2stat}<br>${f3stat}<br></div>

Servlet sends response as follows,

String f2 = "Uploaded file " +fileName+ " at " +uploadedFile.getAbsolutePath();;
request.setAttribute("f2stat", f2);
RequestDispatcher rd = request.getRequestDispatcher("geco.jsp");
rd.forward(request, response);

Finally, all I want to do is to avoid console refresh so that it will not loose its message history. How to do this?

  • does your servlet regenerate the entire page? It probably does, as you are submitting the form, doesn't it? – Axarydax Mar 13 '14 at 07:08
  • Yes it does. I need that feature as some text is entered into the editor from the uploaded files. –  Mar 13 '14 at 07:10

1 Answers1

0

You could rewrite the upload logic to use ajax request - it's not a lot of work with javascript - use FormData and XMLHttpRequest to send the data to the server, and you'd need to convert your servlet to a web service.

On the other hand, you may attach the already generated data from the console in the request and return them from the servlet (in the current configuration), then prepend them to the new response.

Axarydax
  • 16,353
  • 21
  • 92
  • 151
  • Can't I achieve my task without complicating it? –  Mar 13 '14 at 08:30
  • if you cause a postback then your page definitely does disappear, and along with that the text in console. – Axarydax Mar 13 '14 at 08:47
  • but submitting `old console text` along with the files to the servlet and resending them back with the new page shouldn't be that complicated – Axarydax Mar 13 '14 at 08:47
  • Ya, but I don't know ajax. So I'll to start learning and then implement it. –  Mar 13 '14 at 09:14
  • maybe it should be enough to save old html of the div id="box" with some help of javascript - see http://stackoverflow.com/questions/2530635/jquery-add-additional-parameters-on-submit-not-ajax for example. For start it would be more simple to use `textarea` instead of `div` for console, so it is sent to the servlet with the files – Axarydax Mar 13 '14 at 09:41