1

I am tring to display the values that I placed in an Arraylist in java and pass it to the JSP page and display it in a table row. But the results are displayed in a really bizarre way. Please help me out. I am stuck.

Servlet Code:

    ArrayList al = new ArrayList();
    while(rs.next())       
                  {
                      count ++;
                      String  country = rs.getString("CustomerCountry");
                      String customerid = rs.getString("CustomerID");
                      String TitleofAccount = rs.getString("TitleofAccount");
                      String FirstName = rs.getString("FirstName");
                      String LastName = rs.getString("LastName");
                      String City = rs.getString("City");
                      String Address = rs.getString("Address");
                      String emailid = rs.getString("EmailID");
                      String typeofid = rs.getString("TypeOfID");
                      String Idnumber = rs.getString("IDNumber");
                      String branchid = rs.getString("BranchID");
                      String cardnumber = rs.getString("CardNumber");
                      String bankaccntid = rs.getString("BankAccountID");
                      String currencyid = rs.getString("CurrencyID");
                      String isspeciallimit = rs.getString("IsSpecialLimit");
                      String dailylimit = rs.getString("DayTransactionLimit");
                      al.add(rs.getString("CardNumber"));
                      al.add(bankaccntid);
                      al.add(currencyid);
                      al.add(rs.getString("DayTransactionLimit"));
                      al.add(isspeciallimit);

JSP page:

       <table width="700px" align="center" style="border:1px solid #000000;">
            <tr>
                <td colspan=4 align="center" style="background-color:teal">
                    <b>User Record</b></td>
            </tr>
            <tr style="background-color:lightgrey;">
                <td><b>Account No</b></td>
                <td><b>Card Number</b></td>
                <td><b>CurrencyID</b></td>
                <td><b>Daily Limit</b></td>
                <td><b>Status Limit</b></td>
            </tr>

<%
    if (request.getAttribute("al")!=null)
    {
        ArrayList arr = (ArrayList)request.getAttribute("al");
        for(int i=0;i<arr.size();i++) {
            out.println(arr.get(i) + "<html>&nbsp&nbsp<p></p></html>");
            //out.println("<html>&nbsp&nbsp</html>");
        }
    }
%>

Output:

    [kenya, K, 432342423, , 100000.0000, 0,
    kenya, Kumar11, 78788787878, OOOPP, 100000.0000, 0,
    kenya, Kb1, 001001000095, KES, 500000.0000, null]

I want the results to be displayed as:

    1st row - kenya, K, 432342423, , 100000.0000, 0,
    2nd row - kenya, Kumar11, 78788787878, OOOPP, 100000.0000, 0,
    3rd row - kenya, Kb1, 001001000095, KES, 500000.0000, null
1
  • Some code is missing at the end of "Servlet Code" part. Please complete it, else there is no way to precisely diagnose what is happening. I have my assumptions, but there's no point in making blind guesses. Commented Nov 4, 2016 at 16:14

2 Answers 2

1

Always remember that the output of a JSP is the body of a servlet response, which in this case is HTML. It is essential to review the generated output during development and testing, and reviewing only the result of some other program's -- e.g. a web browser's -- processing of that output is insufficient.

I'm inclined to think that the problem would have become immediately evident to you if you had in fact reviewed the HTML emitted by your JSP, which, if it was generated via the JSP code you present, will have looked something like this

<!-- [...] -->

<tr style="background-color:lightgrey;">
    <td><b>Account No</b></td>
    <td><b>Card Number</b></td>
    <td><b>CurrencyID</b></td>
    <td><b>Daily Limit</b></td>
    <td><b>Status Limit</b></td>
</tr>

kenya<html>&nbsp&nbsp<p></p></html>
K<html>&nbsp&nbsp<p></p></html>
432342423<html>&nbsp&nbsp<p></p></html>
 <html>&nbsp&nbsp<p></p></html>
100000.0000<html>&nbsp&nbsp<p></p></html>
0<html>&nbsp&nbsp<p></p></html>

<!-- [...] -->

(Line breaks added for clarity.)

Such code is grotesquely non-conforming, and it moreover exhibits none of the HTML structure that you should expect to use to represent tabular data (i.e. <tr> and <td> elements).

On the other hand, I don't see how the output you present could come from the JSP code you present, whether you're presenting it raw or rendered. The output looks like what you would get from out.println(arr), as opposed to from printing the list elements one at a time.

Additionally, the output seems not to quite correspond to the servlet code, either, in that it appears to contain six data per record, whereas you show the servlet providing only five per record.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. In Servlet/Java - Wrap all fields into an object, say "Customer"
  2. Use JSTL inside JSP, refer this sample,

    <c:forEach var="customer" items="CustomersList"> <c:out value="${customer.id}" /> <c:out value="${customer.userName}" /> <c:out value="${customer.password}" /> <c:out value="${customer.email}" /> </c:forEach>

CustomersList is the arrayList which you are passing from Servlet to JSP.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.