• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling JSP method from within a javascript function?

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic