Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic JSP action  RSS feed

 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a very beginner question. I would like to have my JSP perform a very simple action. I would like to have a single submit button on the page. When that button is clicked, I would like the JSP to instantiate an obect and execute a method. I am able to create an object and call its method today in a scriptlet:

<%
MyObject m = new MyObject();
m.execute();
%>

However this will run every time the page is loaded. What I want is for this to run only when the submit button is pressed.

Simple code fragments welcome!

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've sort of got it backwards.

JSP executes on the server in order to format an HTML page to send to the client. Once at the client, the user interacts with the page, including clicking buttons that can cause Javascript to execute.

The only way to get server-side code to execute is to submit a new request.

So, in essence, any JSP code will always execute on page load and can never be executed as a result of a client-side action on the same page.
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, would I submit the JSP to itself then?

ex. <FORM name="myForm" action="myJSP.jsp" ... />

I was told this would be simple to do but now I am not so sure.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is simple:


Every time the page is loaded, you test to see if the user has clicked the button. If the requestParameter for "enter_button" is null, then they did not. Just present the HTML.

If it isn't null then you know that the the user got here by submitting the form (specifically by clicking the enter_button on that form).
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect! Thanks guys.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!