Question

I'm using struts2 framework(java/js/html/css combo) for my webapp. I am reading a text file from server and I want to write the response to an iFrame present in the same jsp.

Flow: (1) On click of a link, I pass the relative URL of the text file to jsp. (2) When the jsp page loads, the java code in the jsp reads the file from server. (3) Now this response has to be written to an iFrame present in the same jsp file

Can anyone plz help me in writing such response to an iFrame? Thanks in advance :)

Was it helpful?

Solution

[code not tested, only a demostration of the concept]

here's some very rough idea as to how to fix your code, they definitly not the best but they should be enough to help you understand the concept. However I'd still recommend going over the whole concept and maybe come up with a more efficent way to do what you need.

if you insist on using iframe, you need to make use of 2 seperate jsp as W3C says in "Implementing HTML Frames":

Any frame that attempts to assign as its SRC a URL used by any of its ancestors is treated as if it has no SRC URL at all (basically a blank frame).

so you'll need 2 jsp, the first one is basically what you have but the the src of the iframe changed to:

<iframe scrolling="yes" width="80%" height="200" src="second.jsp?content=<%=all%>" name="imgbox" id="imgbox">

and the second one will be something like :

<html><body><%= request.getAttribute("content") %></body></html>

From the code you've shown you forced a "content update" on the iframe by using javascript. The proper/usual way to update an iframe is to provide different input parameter to the second jsp and let it update it for you.

Finally, I'd recommend using JSTL as much as possible instead of scriptlets. It is much cleaner.

OTHER TIPS

What you need to do is set the src attribute of the IFRAME to the jsp url when your link is clicked. Another way to do it is doing something like this:

<iframe src="" name="iframe_a"></iframe>
<p><a href="yourJSPUrl" target="iframe_a">W3Schools.com</a></p>

with the correct parameters of course

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top