File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes how to read html table in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to read html table in servlet" Watch "how to read html table in servlet" New topic

how to read html table in servlet

Atanu Banerjee

Joined: Nov 11, 2008
Posts: 10
Dear Members :

I have a table populated in servlet, with one column as "delete button".
This button when clicked, the row gets deleted and this code is written in a Javascript file. After user alters the table (deleting rows), a checkout button below the table is clicked, and user would be taken to another page (displaying summary info collected from modified table).

When checkout button is going to submit the modified table to another servlet, how this servlet is going to read the modified table ?
Simply put, how a servlet can read (row,cell) of a html table ?

Thanks in advance
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
One way to achieve it would be to embed a form with the table values as hidden elements.

SCJP 1.4 85%
Atanu Banerjee

Joined: Nov 11, 2008
Posts: 10
Are you suggesting hidden select field approach ? Could you please paste some code snippet, what you mean ? Thanks.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
An HTML table can't be submitted to a servlet. What you can submit is an HTML form; so for any write or delete operations you need to construct an HTML form that submits all relevant data to the servlet (maybe the ID of the data to be deleted).
Atanu Banerjee

Joined: Nov 11, 2008
Posts: 10
Can I do something like this to achieve, as I described in my first mail ?

On user's clicking the "checkout" button call a javascript function which will read the (modified) table elements in array and send it to the final servlet, for it to collect & display summary info ?

If someone could please review this idea and post the syntactical summary -do appreciate very much. Thanks.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I would use Javascript to POST a message to the next servlet. Let each line of the POSTed text contain row, column, textdata

1,5,"no way"

the receiving servlet will have to parse the input stream - ie you cant use the regular form style getParameter().

Alternately you could POST a message emulating a form, building a "name" from the row and column and use getParameterMap().

1.5=no way

Srikanth Nutigattu
Ranch Hand

Joined: Oct 30, 2007
Posts: 114
I have a table populated in servlet...

I don't consider making things complex.... Your's is a simple case and it has a very simple solution:

As you said you populate the table using a servlet, I assume that you have the data, more over you are not entering or modifying the data in table but simply "deleting the rows".

If you need I can paste the code for you, but its really simple and I think you can do it yourself.

You can use the following steps:

1. In the servlet that populates the table, along with the rows insert a hidden field "id" and set its "value" to a unique key to identify the row.
(eg: you can use the primary key if each row represents each record in db)

2. You can "Delete the rows" from the table using a simple "Java Sript function" when you click the delete row button.

3. The entire table should be inside <form> and when you click the submit button, all the "hidden fields" will be submitted which represents the rows that are not deleted.

4. Using the ServletRequest's getParameterValues() or if it is Jsp, you can use EL implicit object 'paramValues["id"]' to retrieve the array of submitted "id"'s.

Now, as you got the keys, you can identify which rows are submitted.

As simple as that!

Atanu Banerjee

Joined: Nov 11, 2008
Posts: 10
Thanks everybody for eloquent suggestions. But I adopeted William's getParameterMap() approach - thinking of long term usage of this application.

I'd be still interested to know the syntax of sending/receiving of array from/to javascript/servlet. Could someone throw some light on this please ?

Thanks very much.
I agree. Here's the link:
subject: how to read html table in servlet
It's not a secret anymore!