Matt-
I'm sure you realize that you can't simply make a bean on the server
react to mouse clicks on the server. After all, at the time the user is
clicking, the JSP that generated the page has finished running and the
server and the browser are disconnected from each other.
I believe I have a pretty hacked way for you to do what you want, but first,
let me suggest that you don't do it. If I understand you correctly, you want
something like a remote procedure call where you leave the web page exactly
as it is and do something behind the scenes. Most web pages don't work that
way, and there is very little support for doing things that way. You would
probably be happier if you redesigned your UI to work more like other web
pages, where you redisplay the page. I have been down the other road and it
is not pretty.
Second, if you don't want to change, might I suggest that you use an applet
to communicate with the server. That way, you can at least have a little
control over the interactions with the server. You can call a method in an
applet that uses a URLConnection to talk to a servlet or a JSP and reads a
response back.
Now, my last suggestion is to use the old "hidden frame" trick. You open up
a URL and tell the output to go to a hidden frame. You are still opening up
a new page, but since it goes to a hidden frame, the UI looks exactly as it
did before. Here is a short 4-file example:
MainPage.html
==========
<HTML>
<FRAMESET ROWS="100%,*">
<FRAME name="main" src="ShowInsert.html">
<FRAME name="target" src="blank.html">
</FRAMESET>
</HTML>
ShowInsert.html
===========
<html>
<script>
function doInsert(person, name)
{
// Invoke the JSP on the server side
window.open("doInsert.jsp?person="+escape(person)+"&name="+
escape(name), "target");
}
</script>
<body>
<table>
<tr>
<td><input type="button" value="insert" onclick="doInsert('person1',
'name1')">
<td>Person 1<td>Name 1
<tr>
<td><input type="button" value="insert" onclick="doInsert('person2',
'name2')">
<td>Person 2<td>Name 2
<tr>
<td><input type="button" value="insert" onclick="doInsert('person3',
'name3')">
<td>Person 3<td>Name 3
<tr>
<td><input type="button" value="insert" onclick="doInsert('person4',
'name4')">
<td>Person 4<td>Name 4
</table>
</body>
</html>
doInsert.jsp
========
<%
String person = request.getParameter("person");
String name = request.getParameter("name");
// Do the Java stuff here to insert person & name into the database (I
omitted this for the sake of brevity)
%>
<html>
<script>
// This runs on the client after the request has been completed
function handleLoad()
{
alert("Inserted <%=person%>,<%=name%> into the database");
}
</script>
<body onload="handleLoad()">
</body>
</html>
blank.html
=======
<HTML><BODY></BODY></HTML>
I use blank.html because I have had problems making an empty frame on
Netscape without specifying an SRC. There's probably an easier way, I just
haven't taken the time to find it.
Mark
----- Original Message -----
From: "Matt Brown" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, March 17, 2000 12:14 PM
Subject: writing to db on button click
> I want to insert some buttons on a jsp page, that when clicked would each
> write a different predetermined row of data into a db. Is it possible to
do
> this without using a form and servlet that would redisplay the page? I
want
> the buttons to interact with db methods directly without affecting
display.
>
> Is it possible to access beans via a bean id such as:
> out.println(" <TD><input type=\"button\" name=\"match\" value=\"match\"
> onClick=\"beanid.InsertRow()\"> ");
> ?
>
> Thanks,
> Matt
>
>
===========================================================================
> 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
===========================================================================
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