• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Calling Java Method

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am having some problem whereby i want to call a java method by pressing a button in jsp. can anyone teach me how can i click the button and then call the method out? thanks
 
Sheriff
Posts: 67671
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
You'll need to be more specific.

Your HTML page is sitting on the client in a browser. Where is the Java method you want to call? In an applet? On the server? Somewhere else?
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's on the server. not applet. after i have click the button in the JSP page. i want to perform a method in the java class file.
can guide me on this?

thanks alot!
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you should read some tutorial about servlets and jsp, because it would be too long to explain here.
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, you want to obtain user input, eh?

I put together a simple little multimedia tutorial, along with sample code, that shows you how to do just that:

Obtaining User Input with an HTML Form, a Servlet and a JSP

The key is your form:

<FORM method="post" action="NumberGuesserServlet">
<INPUT type="text" name="guess" size="10">
<INPUT type="submit"
name="submit" value="SUBMIT"></FORM>

And your Servlet/JSP obtaining data from the form:

public class NumberGuesserServlet extends HttpServlet implements Servlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String magicNumber = System.currentTimeMillis() % 9 + 1 +"";
String guess = request.getParameter("guess");
if (guess.equals(magicNumber)){
response.getWriter().print("Correct! The magic number was: " + magicNumber);
}
else {
response.getWriter().print("Sorry, the magic number was: "+ magicNumber);
}
}

}


Enjoy the Tutorial!

-Cameron McKenzie
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Cameron. The interactive tutorial was very helpful. But there is still a small minor problem because we are using javabean as it is easier for a beginner like me. sorry about that. so just want to ask whether is the method use between servlets and javabean? thanks alot





i want to call the setData because after i have get the data from the input box from bean. i use the javabean to set and get it. after that i want to call the java method to store it..
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
Use the below code

<jsp:useBean id='name' class='/classname' scaope='page'/>
<%name.method();%>
 
I am displeased. You are no longer allowed to read this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic