0

I have a JSP page, where an editor (ACE editor) is based on a DIV tag.

1. User upload a file (ex: my.txt)
2. This file is saved in the servlet (WEB-INF/my.txt)

Now, I want to open this file, copy it's contents to a variable in Java Script and populate the DIV tag.

How do I do this?

Based on an answer below, I've understood that once I get the contents of the file I can populate my DIV tag by using

var MyDiv1 = document.getElementById('DIV1');
MyDiv1.innerHTML = yourFileContent;

This solves the second part of the problem, now how to I open that file and copy its contents to a var in JS?

====================================EDIT==========================================

From the answers below, I've done the following steps

Step1: Getting the file contents, I take 4 input files so I've included a file counter to identify which file is being uploaded and when the 4th inpiut file is uploaded its contents are being stored to a string variable.

Servlet.java

response.setContentType("text/html");
String LINE = "<br>";
String filename = "/WEB-INF/myfile.txt";
fileTxt = "";
ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream(filename);
if (is != null) 
{
 InputStreamReader isr = new InputStreamReader(is);
 BufferedReader reader = new BufferedReader(isr);
 PrintWriter writer = response.getWriter();
 String text = "";
 while ((text = reader.readLine()) != null) {
 fileTxt = text + LINE;
 writer.print(fileTxt);
 HttpSession session = request.getSession(true);
 session.setAttribute("FileText", "fileTxt");
 }                              
 }

Step2 : Submitting the forms for upload and right after submitting accessing the variable to populate ,

my.jsp

document.myform.submit();
var name = '<%= session.getAttribute("FileText") %>';
var div = document.getElementById("editor");
div.innerHTML = name;

PS: This still displays a null value, working on it and waiting fr answers.

  • you want to populate what ? the folder content and put it inside the div ? –  Feb 28 '14 at 12:44
  • I want to populate the
    tag value with the contents of the file
    –  Feb 28 '14 at 12:45
  • So basically I want to copy all contents from a file to the
    tag value
    –  Feb 28 '14 at 12:45
  • please post how you are getting the file content from server ? from Servlet right ? –  Feb 28 '14 at 13:14
  • what the document.myform.submit(); is for ? i mean here ? –  Mar 03 '14 at 13:17
  • Hey its for submitting the upload files from tags –  Mar 03 '14 at 13:18
  • you mean you want to display the content after the submit ? it doesnt work this way –  Mar 03 '14 at 13:20
  • on submit, the contents of the uploaded file are written into a text file and the contents of the same are being copied above in servlet.java and once that is done I want to push those values into my
    –  Mar 03 '14 at 13:23
  • I can't do this amro? –  Mar 03 '14 at 13:23
  • the submit goes and save the file , then you get the file content put it on a session and redirect to the other page where the session will be found . and then you can use the session attribute –  Mar 03 '14 at 13:28
  • Submit -> put in session -> use session I guess this is what I did above, and yet it displays null. –  Mar 03 '14 at 13:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48878/discussion-between-zedai-and-amrola) –  Mar 03 '14 at 13:29

0 Answers0