Tunca Ersoy
Tunca Ersoy

Reputation: 339

out.print not working in java method in jsp

I have a pretty weird problem, here it goes:

I have a jsp page, in this jsp, there is an iframe showing some content from another jsp. I want this iframe to be refreshed in the <body onload="blabla"..>

So what I tried to do is, I converted the static HTML iframe code into a java code in a method. So it was like this:

. . html here ...

<iframe ... blabla>
</iframe

html here.. . .

And I did it like:

<body onload="refreshiframe();">

. . html here ...

<%! void refreshiframe()
{
out.print("<iframe.. blabla> </iframe>");
}
%>

html here ... . .

But the problem is, the out.print inside refreshiframe gives compile error. My compiler, jdeveloper 10g, specifically says that "variable 'out' not found". I can use out.print outside of the method but not inside the method. How can I use out.print in the refreshiframe() method ? or is there a better way to solve this problem ? Thank you.

Upvotes: 0

Views: 1919

Answers (3)

BalusC
BalusC

Reputation: 1109735

Indeed, you're confusing Java/JSP with JavaScript.

The <body onload> should point to a JavaScript function, not to a Java/JSP method. If I understand you right, you want to refresh/reload the content of an iframe? If so, add the following to the <head> of your HTML document.

<script>
    function refreshiframe() {
        document.getElementById('frameId').contentWindow.location.reload();
    }
</script>

And give your <iframe> element an id="frameId".

<iframe id="frameId"></iframe>

Upvotes: 4

Costi Ciudatu
Costi Ciudatu

Reputation: 38265

Just declare that method to take a parameter and then invoke it as refreshiframe(out).

EDIT: I totally missed that you called this method as a JavaScript event handler. (See Joachim's answer for a good explanation)

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 308269

You are trying to execute a Java method declared in your JSP as a JavaScript method in your resulting HTML.

That won't work.

Look at how your request is handled:

  1. The client (browser) sends a HTTP request to the server
  2. The server handles that request by executing the JSP (i.e. your Java code)
  3. The result of the JSP (some HTML code) is sent to the client
  4. The client displays the resulting HTML, potentially executing JavaScript code in it

As you can see, your refreshiframe() method doesn't even exist at the time (and place) where the onload attribute is interpreted (step #4).

Also, regarding the compilation error: you define a new method in your JSP. That method can only access its parameters, so if you want it to print something to the output, you'll need to pass out to it as an argument.

Upvotes: 4

Related Questions