How to map server response retrieved in jsp to an iFrame - java

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 :)

[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.

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>W3Schools.com</p>
with the correct parameters of course

Related

Call JSP function with JSoup

I'm trying to scrape a .jsp webpage with Jsoup. The page I'm trying to scrape is basically a 6 steps form, filling each form takes me to another one but to do so I must click a button that calls a jsp function. I don't know how to do so with jsoup.
So here is a piece of the jsp page I'm trying to scrape.
<img id="nextButtonId" onkeypress="disableButtons(this);onIntroductionFormSubmit()" height="50"
alt="suivant" title="suivant" src="/eAppointment54-etrangers/element/images/buttons/next.gif"
**onclick="disableButtons(this);onIntroductionFormSubmit()"** onmouseover="downNextSrcPicture(this);"
onmouseout="upNextSrcPicture(this);" style="display: block;">
I want to call the onclick methods onclick="disableButtons(this);onIntroductionFormSubmit(). Maybe there is a way I can do this without having to simulate the actual clicking.
Thanks in advance !!!
Jsoup is not a browser engine, it is just an HTML parser/writer. To do such a thing you should do one of these, I recommend the first one:
Implement the method yourself (it's probably just an HTTP call)
Use an automation library like Selenium (no experience)
(Not recommended) Run a full browser environment such as JavaFX WebView and inject code to do the action.

how to send parameters to jsp

So I'm using JFree chart and I'm trying to generate an image and display it in my jsp code. I would like to know how to accomplish this. I have tried multiple different ways but it doesn't work.
I saved the file and then when I try to access it. It is stored on the webserver and therefore I don't have the url to it?
Any help would be much appreciated.
I also tried to do something like this,
<IMG SRC="chart.jsp"
WIDTH="300" HEIGHT="300" BORDER="0" USEMAP="#chart">
which is basically jsp which generates an image. It works but how can pass parameters to it?
You shouldn't use a JSP, but a servlet to generate an image. JSPs are view components, whose role is to generate HTML markup from a model prepared by a controller.
But anyway, you pass parameters to a JSP the same way as you do for any other URL:
<img src="chart.jsp?param1=value1&param2=value2" .../>
Instead of thinking of the img source as static file think of the url being a stream that will be returned from a servlet using a prticular URL
eg
<img src="/jfreeServer?param1=123"/>

How to present a pretty printed Java source located outside the HTML file?

I am looking at writing a tutorial for a Java concept where it would be really nice if I could write the tutorial as a HTML-document with pretty printed Java sources.
I understand I can do this with e.g. http://code.google.com/p/google-code-prettify/ if I copy the various Java sources in my HTML document where I want them to be and put a styling class on the surrounding tag.
However, in order to ensure that the snippets are up to date I would really like to have the HTML page refer to the actual, real Java source files instead of a manually maintained copy.
To my understanding - which may be wrong - this is not supported directly by the Google Prettyprint library, but perhaps some trickery with Javascript pulling in the file and putting it in the DOM tree inside a <pre> tag could do it? I would like the HTML file to be present in the local file system, so doing server side scripting is not an option.
My question is - how can I do this?
(I intend to have the HTML file physically placed at the root of the source tree. This mean that all references from HTML to Java sources will be relative and without '..'. I do not know if that is important or not.)
There is no way to access files directly using JavaScript. JavaScript is restricted in this way for obvious security reasons.
You will need your webserver to serve the Java files. You don't need to do server side scripting but the content of your Java files has to be available at some web address. If they are you can load the content of the Java files with AJAX and inset the content into your webpage.
Using jQuery loading the text could be done as follows
$.get('java/somefile.java', function(data) {
$('#sourceCodeDestination').html(data);
// Prettyprint neeeds to run again in order to see the newly added code
prettyPrint();
}, "text");
This will load the url java/somefile.java get the content of it as plain text and insert it into the DOM element with the id sourceCodeDestination. For more information see the jQuery documentation on get() and ajax().
Here is a demo. As you can see it loads a minified version of the Prettyprint sourcecode from a CDN and pretty prints it.
if your users can accept the requirement of online access while reading your document, you could host your code somewhere like gist (https://gist.github.com/), and embed it in your html dopcument (see example by putting this into your document <script src="https://gist.github.com/sangohan/6494440.js"></script>)
Assuming prettify.js has been loaded previously you can invoke the function prettyPrint which takes arguments callback and rootNode.
<div id="foo">
<pre id="bar"></pre>
</div>
var pre = document.getElementById('bar');
pre.textContent = 'function () {\n return;\n}'; // assign code
pre.className = 'prettyprint'; // assign class
prettyPrint(null, document.getElementById('foo')); // prettify
DEMO

How to grab a whole body unless header and footer of another site page

I'm developing with Liferay portal.
And now I'm facing a little problem:
I'm making site for some Company that has subsidiaries.
Then, I must cut out some parts(precisely header and footer)
of other site(sub. site) and put the body of page without'em in iframe of main site.
I was "googling", looking for something about Grabbers.
but I've found just about how to grab with PHP or Perl.
and here
It doesn't seem to be exact what I need.
You can try the WebProxy portlet for this.
As you'll have to modify the external content's body, you can't simply show it in an iframe, so this portlet might be what you need. It doesn't work with an iframe internally and you can replace some content on-the-fly.

Access Javascript from Java

I m trying to access JavaScript function from Servlet code. But I'm getting the error shown below.
Here is the code:
out.println("<FRAME src=\"javascript:parent.newWindow('" + URL+ "') \" scrolling=No noresize />");
And this is the error that occurs in JavaScript:
Object does not support this property or method;
You can't access a Javascript function from your servlet code. Javascript executes on the client (= your user's browser) and the servlet code executes on your server (for example Tomcat, JBoss, whatever you're using).
What are you trying to accomplish with your code? I'm sure there's a simpler way to do it than what you just described.
[edited]
I see you just updated your description, so here's my view:
I'm guessing that you want to display a page to the user and when the page is displayed, you want to open a new window which will display another page using the URL parameter to point its address. If this is the case, you should probably just do this in the first page's onLoad() Javascript event using window.open().
There is no newWindow property on a window object (which is what parent references), so this is not unexpected.
Maybe you are looking for the open method instead?
If so, then:
Putting it as the src of an iframe is a very strange thing to do
It will probably be zapped by pop-up blockers
Ok. You try to generate javascript code inside Servlet code. When you do, your code goes to Web browser and it's seen there as a html document with javascript inside. So, your error rather comes from web browser and links to javascript error. Probably it's newWindow method. To open new window you should call window.open() function, I guess.

Categories