简体   繁体   中英

How to pass a Java String to a JavaScript function in a JSP page

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>

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM