Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling Java Method  RSS feed

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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
 
Author and ninkuma
Marshal
Posts: 66612
161
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Hi Eric,
Use the below code

<jsp:useBean id='name' class='/classname' scaope='page'/>
<%name.method();%>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!