Event Handling

Hello,

I am new in JSP. I want to design one master entry form in jsp having Add,delete,edit,cancel & exit buttons on form. Records are coming from database.

Can anybody give me one example program related to it.

Atleast tell me, How to handle onclick event of button in jsp?

Thanks in advance.

[329 byte] By [Smita_Jadhava] at [2007-11-27 4:04:42]
# 1

> Hello,

> I am new in JSP. I want to design one master entry

> form in jsp having Add,delete,edit,cancel & exit

> buttons on form. Records are coming from database.

> Can anybody give me one example program related to

> it.

> Atleast tell me, How to handle onclick event of

> button in jsp?

>

> Thanks in advance.

You can hav a servlet to perform all this operations.

ex...

in your jsp you will have a link.

<a href="/products/producthandler?action=delete&productId=123"> Delete Product </a>

Here now product handler can get product id from request parameters, and based on action it will decide wat to do...

your servlet may look like this

protected void service() {

String action = request.getParameter("action");

String productID=request.getParameter("productId");

if(action.equals("delete"){

//delete product

}

}

No javascript event handling ...

Sudhir

http://www.jyog.com

Sudhir_nimavata at 2007-7-12 9:09:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

You can't handle the onclick event in JSP simply because there is no onclick event and no button visible to the JSP code.

The JSP code runs and create's the final HTML page that is sent to the browser and which the user see's. Once the page is made, JSP is not involved at all in anyway till you send back the data to the server in the form of a request.

What you need is JavaScript to handle client-side events.

nogoodatcodinga at 2007-7-12 9:09:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Thanks Sudhir,But during addition,I want to pass all the data fields related to particular product to servlet.In this how can I pass all these parameter values?
Smita_Jadhava at 2007-7-12 9:09:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

The solution that sudhir has given you will work for links and not for buttons.

Also, to pass your various fields you'll have to specifically code the query string in the hyper link and that would involve JavaScript to reflect values of textboxes that the user might edit.

The best way would be all your HTML fields which would contain the data should be in a form.

On clicking the buttons, the form would be submitted and you would receive them in the servlet using request.getParameter(parameterName);

You can decide what action to perform depending on the button that was clicked. To do this, you will have to name all the 5 buttons that you have with the same name, like maybe 'btnOperation' and values like 'Edit', 'Delete', 'Modify' etc.

In your servlet, you'd say

String operationToPerform = request.getParameter("btnOperation");

if ( operationToPerform.equals("Edit") )

//perform editing here

else if ( operationToPerform.equals("Delete") )

//delete records here

/*

.

.

. and so on...

*/

nogoodatcodinga at 2007-7-12 9:09:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...