I need to return an html code from a JAX-RS endpoint. I followed some tutorials and got to know that I can return it as a String. But the problem is I need to add JavaScript function to that returning HTML. How can I do it?
My code snippet.
return "<html><body>Hello World</html></body>"
Here you can add javascript function inside the returning String. You can follow the same steps following when declaring a javascript inside a html page.
I will provide an example:
return "place your script here Hello World"
Make sure to put the script inside tags.
Related
I'm working on selenium and I have to automate tinyMCE in my web page.
I tried many ways but always get null for code below:
(String)js.executeScript("tinyMCE.activeEditor.getBody().isContentEditable");
You need to add return to the front of your JS script, e.g.
(String)js.executeScript("return tinyMCE.activeEditor.getBody().isContentEditable");
I am working with JSP pages and I need to .append() into a DIV element some java codes.
$("#myDiv").append("<% out.println("ali"); %>");
The previous code is actually wrong because there's quotes, so I escaped them.
$("#myDiv").append("<% out.println(\"ali\"); %>");
But I wasn't successful, nothing was appended to #myDiv
This is correct way to append. If nothing is appended, then the div might not exists in the page.
Try using firebug and check what6 is the output of console.log($('#muDiv'))
Your code should work, it sounds that jQuery is not loaded correctly or there is another problem.
http://jsfiddle.net/5PTwN/1/
Try this
$("#myDiv").append('<% out.print("ali"); %>');
below line also should work because java code will execute server side and inside append will be replaced with "ali".
$("#myDiv").append("<% out.print("ali"); %>");
I was going through some java code(which uses playframework) that provides some shopping cart functionality.The html page contains some javascript (it uses jquery) which is something I have very little knowledge of.Looking through the html,I found a table like this
<table>
...
<td>
buy
</td>
...
</table>
(The template uses ${} to get dynamic values thru playframework template language)
In the entire code ,the only addItem(itemId) method I could find was in the ShoppingCart class which has this signature
public void addItem(Item item) {
...
}
When I searched for onclick ,I found this page
onclick doc,which gives the syntax of onclick as
onclick="SomeJavaScriptCode"
Going thru all the javascript code(it uses jquery and ejohn-templating)I couldn't find any method like 'addItem'
So,how does the onclick work?How would it know which method to invoke?Will it look in the entire code (javascript and java)to find a matching method?
somewhere you have an addItem method in your javascript code. The code in the html is executing that method, not the addItem method you found in the java code.
First is it would only look for javascript methods and not java methods. Second is you can use debugging in firebug to reach to the method called.
It is relatively simple.
It looks like your Java code is executed on the server side, and the JavaScript (including jQuery) is executed on the client side. So the ${item.id} is executed on the server, and onClick executes whatever javascript code you provide for it on the client side.
I do not know playframework, but the addItem should be javascript, and public void addItem(Item item) is java code. Both are not related
This addItem javascript function is somewhere. If you are unable to find it in your code base, you can use the console of your browser (or firebug on Firefox) to see this javascript function. Go to the Script tab and search for addItem.
Javascript can't access server side functions unless is via AJAX or something like it. Even then, it doesn't have direct access to the funcions, but to a page that access that.
Look more carefully for that function in your Javascript using firebug, or the google chrome console. It should be there.
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.
I need to call from one JSP to another, do some stuff over there..
I am inside the caller jsp, inside his handleReqeust(HttpServletReqeust request)
method I am trying to forward the request to another JSP, to call the other JSP file to his handleRequest(HttpServletReqeust request) off course with the request object
I tried it:
RequestDispatcher dispatcher = request.getRequestDispatcher("/theSecondJspFile.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
but to make it work I need for it the object response, but I don't have it,
I am sure I missed something basic, but what?
From my question you can see, I don't have solid backround in java, so please correct me, or refer me to good guide if you feel it necessary
Thanks.
-------------------Edit--------------------------------------
I don't need a redirect, I just want to call another JSP file to his handleRequest method
I think it relate to HTML
What I can make out of your question is that you need to redirect to a secondpage.html from firstpage.html with some data. You can use both GET or POST method to send the data.
For the GET method just redirect to secondpage,html?data=value. The data will be available in the HttpRequest parameter in the controller of the secondpage.html where it can be used as required.
For the POST method you would need to post the data (using a form on firstpage.html) to secondpage.html. In the controller of the secondpage.html the data should be available in a similar way as before.
To include another JSP in a jsp :
<jsp:include page="foo.jsp"/>
You can find some reference material here.
I can not be sure, but what I think you are trying to do is to include a jsp page on to your jsp page and use the the objects and other variables declared in the first jsp page in your second.
<%# include file="mypage.jsp" %> should help you do this.
If this is not what you are looking for, please make your question tad bit more clear. some code will help really.
JSPs are supposed to be used to present the final result. JSPs are not supposed to be part of business tasks leading to this result. There you use normal Java classes for, starting with a servlet class. Let the HTTP request (the one which you enter in browser address bar or specify in some HTML link or form) point to the URL of the servlet instead. This way you can write Java code in the servlet the usual way to invoke other Java classes/methods and finally forward the request to a certain JSP file based on the outcome of the result and then just let that JSP present the final result.
To start with servlets, I'd suggest to read our Servlets info page.
I just added empty Iframe, and set his URL when I needed to call him