Sure, if it's acceptable for the user to see the page refresh, this is one
solution. But why not at least be a little more efficient and invoke the
same JSP that you're currently on, passing a parameter that tells it to call
the reserveChairs method at the top? There's no need to go through a
redirect. And it also allows you to give some sort of feedback, telling the
user what just happened.

In fact, why bother with JavaScript and onClick at all? Simplify the whole
thing to something like this. Heck, let's even allow the user to specify the
number of chairs to reserve:

<%
if (request.getParameter("submit") != null) {
   ReserveBean1.reserveChairs(request.getParameter("numChairs"));
%>
   <font size="+2"><%=request.getParameter("numChairs")%> chairs have been
reserved!</font>
<%
}
%>

<form action="<%=request.getRequestURI()%>">

Number of chairs to reserve: <INPUT TYPE="text" NAME="numChairs" SIZE=30>

<input type="submit" name="submit" value="Reserve Chairs">

</form>

But of course, it would be better JSP programming if the call to
reserveChairs was done using the jsp:setProperty tag. I'll leave that as an
exercise for the reader.

--Jim Preston


-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Peter Gozal
Sent: Monday, July 24, 2000 9:59 PM
To: [EMAIL PROTECTED]
Subject: Re: in JSP: Call a method of a Bean when button is clicked


Actualy I think you can put something like this
in your OnClick JavaScript handler:

        onClick="location='beancaller.jsp';"

Where in your beancaller.jsp you
call your bean methods:

<%ReserveBean1.reserveChairs() %>

and redirect back to the page where you click
the button or somewhere else.

Peter

> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of David Eaves
> Sent: 09 Agustus 2000 4:26
> To: [EMAIL PROTECTED]
> Subject: Re: in JSP: Call a method of a Bean when button is clicked
>
>
> You are a patient man Jim.
>
> Dave
>
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Jim Preston
> Sent: Tuesday, August 08, 2000 6:04 PM
> To: [EMAIL PROTECTED]
> Subject: Re: in JSP: Call a method of a Bean when button is clicked
>
>
> Is there a solar storm or something that's causing all these
> Java-from-JavaScript questions all at once?
>
> Marcel, you haven't been reading the list very carefully the last
> few days,
> have you? I've addressed this question in slightly different forms no less
> than three times in the last week. The OnClick handler is
> JavaScript, living
> on the browser, being called by the browser when the user clicks
> the button.
> The "<% . . . %>" is a JSP scriptlet tag that gets executed ON THE SERVER
> when the page is being served. The call to ReserveBean1.reserveChairs()
> happens ON THE SERVER when the page is being served. The result
> of that call
> gets put into the text that's being sent out to the browser. Later, in a
> completely different environment, when the user clicks the button, the
> browser executes the JavaScript for the OnClick handler, which is going to
> be the string value of whatever your reserveChairs method returned and
> almost certainly isn't going to be meaningful in JavaScript. Do a "view
> source" on that page from the browser and you'll see.
>
> Ok, I'm done with this subject. If no one else takes up the mantle, people
> who don't get it will be on their own.
>
> --Jim Preston
>
>
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Marcel van Kooy
> Sent: Tuesday, August 08, 2000 5:22 AM
> To: [EMAIL PROTECTED]
> Subject: in JSP: Call a method of a Bean when button is clicked
>
>
> Hi all,
>
> Can please somebody help me with the following problem?
>
> I want to call a method of a JavaBean when a button is clicked. What is
> the syntax of this?
> The following code I made is not working:
>
> TD VALIGN=TOP WIDTH=102><INPUT TYPE=Button  ID="DBStyleReserveerButton"
> NAME="ReserveerButton" value="Reserveer"  Class="ArialBlackButtons11"
> tabIndex="0"></TD>
> <DEFANGED_SCRIPT FOR="ReserveerButton" EVENT="DEFANGED_OnClick" <%
> ReserveBean1.reserveChairs() %>;</script>
>
> Best regards,
> Marcel van Kooy
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to