uniswift
uniswift

Reputation: 21

How to fetch table values from jsp which is created using Javascript in servlet

table is:

sr.no  col1  col2  col3 
1       a     b     c

in jsp.. . . I have created new row using Javascript

for ( var i = 1; i <columnCount; i++) {
var cell1 = row.insertCell(i);
var element1 = document.createElement("Input");
element1.type = "text";
cell1.appendChild(element1);}

now table is..

sr.no  col1  col2  col3 
1       a     b     c
2       x     y     z

now my question is... how we will fetch these values.... viz... 1)for col1:: a,x 2) for col2:: b,y 3) for col3:: c,z.... in servlet??

Upvotes: 0

Views: 2681

Answers (3)

juicer_java
juicer_java

Reputation: 1

you can resove it in two ways : (1)dynamic parameter at form ,in servlet can use for circulation get it (2)at jsp page you can use javascript get datas and join them by special flag,use an parameter submit ,in servlet you can get data and use flag split.

Upvotes: 0

Anup Singh
Anup Singh

Reputation: 1563

Inside javascript assign name to each input element , You have to use form tag, put your table inside form, and on form-action.do read post variables

JAVASCRIPT

element1.name = "input_name_"+i;

HTML

<form action="form-action.do" method="post">
<table>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
    <thead>
        <tr>
            <td colspan="3"><input type="submit"></td>
        </tr>
    </thead>
</table>
</form>

JAVA

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String firsrElement = request.getParameter("input_name_1");
        String secondElement = request.getParameter("input_name_2");
}

Upvotes: 0

The Dark Knight
The Dark Knight

Reputation: 5585

The standard way of passing/submitting data to the server in the pure Servlets/JSP world (as in your case from the JSP to the servlet) is by using HTML form, i.e. the same way as when using other technologies (ASP.NET, PHP etc). And it doesn't matter whether it is a pure HTML page or JSP page. The recommended/most used method of submitting data from the form to the server is POST.

You also can pass data in the query string that is contained in the request URL after the path (this also happens when instead of POST you use GET method in the form). But that is for the simple cases, like constructing URLs for the pagination etc (you can see the example of constructing URLs with the additional queries here: Composing URL in JSP) Example of passing parameters in the URL: http://example.com/foo?param1=bar&page=100

For the difference between submitting data using GET and POST methods read here:

GET versus POST Requests on HTML Forms

In HTML forms, what’s the difference between using the GET method versus POST?

So you can configure some servlet to process data sent/submitted from a JSP or HTML etc. It is highly recommended to submit data using POST method and respectively to process the submitted data using the doPost() method in your servlet.

Upvotes: 1

Related Questions