1

I am looking to pass a string from my Java to a javascript showContent function. Both the Java and javascript are contained in a JSP page. The string strLine contains XML content I want to display using the showContent function.

My Java

        try{    
        //Open the file that is the first command line parameter
            FileInputStream fstream = new FileInputStream(table.get(xmlMatch));                 

            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

        //Read File Line By Line
            while ((strLine = br.readLine()) != null)   
            {
                out.println (strLine);
            }  

The Javascript (I have to credit Peter for supplying me with this in another question)

<script type="text/javascript" language="JavaScript">
function showContent()
{
document.getElementById('showContent').innerHTML = "printed content";
}
</script>

I have tried replacing the above "printed content" with "strLine"; (strLine); and ("strLine");

I also tried set strLine as a session attribute using session.setAttribute("strLine", strLine); and using "<%=strLine%>"; but the result was null printed to the screen.

Any help with this would be great.

The HTML

<a href="#" onclick="showContent()">Next! <%=keywords%> concept </a>
<div id="showContent"></div>
3
  • 1
    What content are you trying to print to the screen? It appears that you are eventually assigning a value of null to strLine, by the end of the System.out while loop. Commented Oct 9, 2012 at 15:31
  • The value of strLine is 1 - 2 paragraphs of XML content. - ` out.println (strLine);` currently prints this to the page. Commented Oct 9, 2012 at 15:41
  • 1
    @Deepend To get the value of the session attribute it should be <%=session.getAttribute("strLine")?> and not just <%=strLine%> (which refers to the original variable). Commented Oct 9, 2012 at 16:30

2 Answers 2

3

Instead of printing it with out.println, you should put in a variable (a StringBuilder maybe). In order to do that you have to:

Declare the variable at the right scope (maybe at the beginning of the JSP)

StringBuilder contentInnerHtml = new StringBuilder();

Then append the text of the file to this new variable:

while ((strLine = br.readLine()) != null)   
{
    contentInnerHtml.append(strLine);
}

Finally, in the javascript part of the code return its value (with toString()):

<script type="text/javascript" language="JavaScript">
function showContent()
{
    document.getElementById('showContent').innerHTML = "<%=contentInnerHtml.toString()%>";
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

If your html is within your try/catch block, <%=strLine%> should work fine. If not, then assigning it as a session attribute would work as well, however, you need to access it from the session as well:

ex:

    try{    
    //Open the file that is the first command line parameter
        FileInputStream fstream = new FileInputStream(table.get(xmlMatch));                 

        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

    //Read File Line By Line
        while ((strLine = br.readLine()) != null)   
        {
%>
<div id="xxx"><%= strLine %></div>
<%
            out.println (strLine);
        }  

But that is incredibly ugly code and difficult to read/debug.

    try{    
    //Open the file that is the first command line parameter
        FileInputStream fstream = new FileInputStream(table.get(xmlMatch));                 

        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

    //Read File Line By Line
        while ((strLine = br.readLine()) != null)   
        {
           session.setAttribute("strLine", strLine);  
           out.println (strLine);

        }  
  } // end of try
  catch( ... ){}
%>

<div id="xxx"> <%= session.getAttribute( "strLine" ) %></div>

However, the in the second case, you will only be displaying the last line of your file, so I am not entirely sure what you are trying to accomplish.

If you are hoping to display the full text, perhaps you can use:

        StringBuffer strLineBuf;

    //Read File Line By Line
        while ((strLine = br.readLine()) != null)   
        {
            strLineBuf.append(strLine).append("<br/>");
        }  
        session.setAttribute( "strLine", strLineBuf.toString() );

And then after your try/catch finishes, in your html code:

  <div id="xxx"><%= session.getAttribute( strLine ) %> </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.