Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a java method from class in jsp  RSS feed

 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Anyone knows how we can call a method from java file in JSP using onClick() ??
thank you
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
143
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. Click handlers are triggered in JavaScript on the client, long after the JSP has executed on the server and sent the resulting HTML page to the browser. You'll need to either submit a request with a form or link, or use Ajax to contact the server.

Please read this article to understand how JSP works and why what you ask is not directly possible.
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm..JSP is my topic so i should remain on JSP. So, i have to call the method from JSP and then create new JSP file where just will forward to my current page, just to update the client?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
143
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are wanting to react to a click, you cannot do that while the JSP is running. The user doesn't even see your page until after the HTML page has been sent to the browser and the JSP has stopped executing.

Did you read the article?
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just finished it now
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
143
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now you should understand that by the time that a user has the opportunity to click on a link (or button, or any other element), that the browser has disconnected from the server and that the JSP that generated the page is no longer executing.

The only way to contact the server again is through a subsequent HTTP request.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!