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.