javascript onclick in html confusion - java

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.

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.

Add Javascript to jax-rs endpoint

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.

With the click of an HTML button, how to INVOKE a java method AND REDIRECT to another HTML page?

I have a button through which i INVOKE a java method :
<h:commandButton value="My Schedule" action="#{empDutySchedBean.viewMyTask}" rendered="#{welcomeBean.workSchedule}" class="mainLinks"/>
now through this method viewMyTask() i get a List of values returned from the database.
public List viewMyTask()
{
setEmpID((String) session.getAttribute("userName"));
setEmpDuty(ts.getMyTask(getEmpID()));
return empDuty;
}
From here i want tht with the click of a button, the user is redirected to another page and the List data shows up.
I dont understand how to REDIRECT to another HTML page and display the LIST data.
NOTE: --> this button is basically a menu on my webpage. For the other menus i have used an anchor tag. However as you cannot invoke a method using anchor tag, i have used button tag. But because of this, I am not being able to redirect
Some Ways that i think it can be solved are:
use anchor tag instead on button to redirect to the other page AND then use onLoad method of javascript to INVOKE java method
invoke another method after viewMyTask() to perform the redirection.
I dont kw how these methods would work though.
First off, I assume you already know how to return the data Java side, in which case this is really just a javascript question. To do simple post-gets, I usually use Ajax, like so:
new Ajax.Request(myUrlToCall, {
method:'POST',
onComplete: function(transport) {
window.location.replace(theRedirectUrl);
}
});
This will call whatever url you wanted to call Java side, then on the completion of this call, will redirect you to whatever url you want to go to. If you need to send some parameters to the java side, all you need to add is:
parameters:myParamVar,
after the method:'POST' line, and if you want your java side to return the url, then all you'd need to do is send it as json or similar in the response, then do:
window.location.replace(transport.responseJSON["theRedirectUrl"])
Hope that helped!

call java function from JSP page's button on click event

There is an application that stores photos in oracle db (oracle multimedia format). There is a JAVA servlet page that handle the connection and the SQLs ..., and there are JSP pages to show the images in browser.
There is a delete function in the JAVA page, when i click one thumbnail it navigates to the an other JSP and show the full size image. Here i would like to call the
public void deleteRowById( String selectId ) function which is in the JAVA servlet page. i would like to use a button, and i'm not realy know what is the proper way to call this function.
Any suggestions are welcome.
In the mean time i solved the situation. There is a java class that contains SQL statements -taht's what i would like to call with a form button.
1st JSP page show the button like delete, it navigates to an other JSP page via passing a parameter with post method, in this 2nd JSP page i call the main java class method that execute a delete statement then from the 2nd JSP page i navigate back with <jsp:forward page="1st_page.jsp"/>
Maybe not a cutting edge solution but works like charm :)

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