How can I write a HTML-conform String to a new window using a JSP with inner Javascript?
Our Situation:
- Theres a .jsp-File used by a Tomcat-Server
- The .jsp contains a <script language="Javascript>
-Tag
- this script needs to write a complete, HTML-5-valid-String to a new Window using window.open
and 'window'.document.write(htmlString)
Our .jsp-File (schematic):
<%@ include file = "..." %>
<...some page imports...>
<...some scripts...>
(e.g. <script type = "text/javascript" src="...."> </script>)
<%
String strMethod = "foo_Method_Bar";
String strOutput = "";
strOutput = someJavaBean-Call_that_brings_valid_html5_string_with_script-Nodes;
%>
...
<script language="Javascript">
var strHTML = "<%=strOutput%>";
var win = window.open('','','width=...,height=...');
win.document.write(strHTML);
top.close();
win.focus();
</script>
Our Problem (schematic):
- .jsp-File gets called.
- .jsp-File calls a JavaBean and sets a global String-Variable to the HTML-String (including <script>
-Tags, DocType-Declarations, etc.)
- .jsp-File replaces all occurrences of the Variable with the obtained String
- javascript (<script>
-Tag in the .jsp-File) gets Executed
- <script>
-Tag of Javascript is already closed, before 'window'.document.write(strHTML) gets executed, because theres a </script>
-Tag in the Javascript
e.g.
<script language="Javascript">
var strHTML = "<!DOCTYPE html> <html> ... <script> ... </script> ... </html>;
var win = window.open(...);
win.document.write(strHTML);
</script>
- I've searched for quite some time now and I don't have a clue how to solve this issue.
- Writing the HTML-Sourcecode into the Server-File-System and accessing it later on or something like that would be really, really dirty.
- Part of the Problem is that we (of course) have to use this type of javascript-call in the .jsp-File for historical reasons...;-)
- Is there maybe a way to assign the strHTML-Variable in the .jsp-File by Reference only (e.g. without replacing the <%=strOutput%>-Variable)?
Additional Info:
The HTML-String-Generation is used at various places of our programs, multiple times, so i hoped not to have to escape any special CharacterSequences for this particular Call to work.
Appreciate all your help and ideas on this...;-)
Solved: See also Why split the tag when writing it with document.write()? on this issue.