File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Calling JSP method from within a javascript function? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Calling JSP method from within a javascript function?" Watch "Calling JSP method from within a javascript function?" New topic
Author

Calling JSP method from within a javascript function?

Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
Hi,

I would like to call a method written inside a JSP(say JSP2) from within a javascript function written inside another JSP(say JSP1).Please tell me if this is possible and how it can be done?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

It is not possible.

Firstly, only one page is loaded at a time (unless there's something that you're not telling us regarding the use of iframes or some such).

Secondly, even if everything was on the same page, Java on the server and JavaScript on the client cannot interact. To understand why, perhaps this article will be helpful.
[ November 18, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12784
    
    5
I think that instead of:
Java on the server and JavaScript on the client cannot interact.

I would say:
JSP on the server and JavaScript on the client can only interact through the normal servlet request and response. JavaScript talking to servlets asynchronously happens all the time.

Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Right, I should have said there is no direct interaction. Obviously page submissions and Ajax requests can be initiated under JavaScript control.
Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
Guys I basically have 2 JSPS....JSP1 and JSP2

Inside JSP1 I have a javascript function which goes something like:

function callingOne(){
url = "../../myapplication/folder/" + "JSP2.jsp";
var str = window.open(url);
alert("returned string from jsp2 is "+str);
}

Inside JSP2 I have a normal method deined something like:

<%!
String calledMethod(){
String st = "hi there";
....
....do something..
...
return st;

}

Now my problem is why does the string value not get displayed in the alert box of JSP1??It instead displays :
returned string from jsp2 is [object]

instead of

returned string from jsp2 is hi there

please throw some light on this

[ November 19, 2007: Message edited by: Nikhil Sun ]
[ November 19, 2007: Message edited by: Nikhil Sun ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not sure why you think it would.
A call to window.open returns an object reference to the new window, not a string containing that windows's document's text.

Also, a page request to a JSP doesn't necessarily trigger a call to any particular JSP method defined on that page.
Where are you calling calledMethod()?
[ November 19, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling JSP method from within a javascript function?