Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling JSP method from within a javascript function?

 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic