Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:18:24+00:00 2026-06-12T12:18:24+00:00

I am looking to pass a string from my Java to a javascript showContent

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:18:25+00:00Added an answer on June 12, 2026 at 12:18 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking to pass variables from c# to javascript to use some jquery code. Passing
I'm looking to pass a keyword from a HTML page to a PHP page
I'm looking for the best practice on how to pass data from page to
I'm trying I have a string that I pass into my function from user
I am looking for a way to pass a string containg the * character
I'm looking for a way to pass a variable or string or anything into
I am looking for a method to pass property itself to a function. Not
Possible Duplicate: Unchecked exceptions in Java: Inherit from Error or RuntimeException? Looking at how
I am trying to use inheritance to pass a string from one form to
I'm looking to pass a Facebook session across activities. I saw the example from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.