Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to run a method in JSP

 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a botton below named Button1 and a JSP method named method1. I want to be to execute method1 whenever someone clicks on Button1, but I do not know how. If there is a way this can be done please let me know. Any help is greatly appreciated. Thanks...

--------------------------------------------------

<form name="form3" method="post" action="">
<input type="submit" name="Button1" value="Submit">
</form>


<%=
public void method1(){...}
%>
--------------------------------------------------
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the first request getParameter("Button1") returns null so the code in the "if" block is ingored.

When you click on the button, the form is submitted back to the server.
This time getParameter("Button1") will return "Submit".
Since the return value is not null, the code in the "if" block is executed.

NOTE: Putting the logic right in the JSP is called Model1 web programming and is highly discouraged these days. Once you get a grip on the whole request/response nature of web programming, you might want to look into model2 or the MVC architecture.
 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there another way, which does not involve reloading the page, but directly runs the method?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you're getting confused about how web applications work...

The method is on the server. The button is on a web page on the client.

There's no way the button can call directly to the method without making a new request. The problem is, requests generate some kind of response, and most responses usually refresh the page.

You could use AJAX to make a "background" request to the server to invoke the method without refreshing the page.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic