我正在尝试使用Arraylist将数据从Servlet显示到JSP。现在,我在以表格格式显示数据时遇到了一些问题。请有人帮我一点。
<%--
Document : customer
Created on : 10 Oct, 2016, 10:19:31 AM
Author : user
--%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HomePage</title>
<script src="JavaScript/SpryMenuBar.js" type="text/javascript"></script>
<link href="styles/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
</head>
<body>
<ul id="MenuBar1" class="MenuBarVertical">
<li><a href="#">Update Limit</a></li>
</li>
<li><a href="#">Delink Account</a></li>
<li><a href="#">Edit CardNumber</a>
</li>
<li>
<a href="#">Linked Accounts</a>
</li>
<li><a href="#">SMS Alerts</a>
<li><a href="#">Join Account</a>
</ul>
<p> </p>
<p> </p>
<form method="post" action="Search">
<hr />
<hr />
<p>
Client Details
<hr />
<hr />
</p>
<p>Mobile Number:-
<label>
<input type="text" name="mobilenumber" id="mobilenumber" />
</label>
</p>
<p>Country:-
<label>
<input type="text" name="country" id="country" />
</label>
</p>
<input type="submit" formaction="Search" name="New" id="New" value="Search" />
</form>
</html>
<html>
<form method ="get" action="Search">
<p> Title:-
<input type="text" name="Title" id="Title" value='<%=(request.getAttribute("TitleofAccount")==null)?"":request.getAttribute("TitleofAccount")%>' />
</p>
<p>Middle Name:-
<label>
<input type="text" name="MiddleName" id="Middle Name" />
</label>
</p>
<p>Address:-
<label>
<input type="text" name="address" id="address" />
</label>
</p>
<p>City:-
<label>
<input type="text" name="City" id="City"value='<%=(request.getAttribute("City")==null)?"":request.getAttribute("City")%>'/>
</label>
</p>
<p>Type of ID:-
<label>
<input type="text" name="Idtype" id="Idtype" value='<%=(request.getAttribute("typeofid")==null)?"":request.getAttribute("typeofid")%>' />
</label>
</p>
<p>Elma ID:-
<label>
<input type="text" name="elmaid" id="elmaid" value='<%=(request.getAttribute("customerid")==null)?"":request.getAttribute("customerid")%>'/>
</label>
</p>
<p>First Name:-
<label>
<input type="text" name="firstname" id="firstname" value='<%=(request.getAttribute("FirstName")==null)?"":request.getAttribute("FirstName")%>' />
</label>
</p>
<p>Last Name:-
<label>
<input type="text" name="LastName" id="LastName" value='<%=(request.getAttribute("LastName")==null)?"":request.getAttribute("LastName")%>'/>
</label>
</p>
<p>Registration Branch:-
<label>
<input type="text" name="registrationbranch" id="registrationbranch" />
</label>
</p>
<p>Email address:-
<label>
<input type="text" name="emailid" id="emailid" value='<%=(request.getAttribute("emailid")==null)?"":request.getAttribute("emailid")%>' />
</label>
</p>
<p>ID No:-
<label>
<input type="text" name="idno" id="idno" />
</label>
</p>
<hr />
<hr />
<p>
Account Details
<hr />
<hr />
</p>
<p>Bank A/C id :-
<label>
<input type="text" name="accountid" id="accountid"value='<%=(request.getAttribute("bankaccntid")==null)?"":request.getAttribute("bankaccntid")%>' />
</label>
</p>
<p>Transaction Limit:-
<label>
<input type="text" name="transactionlimit" id="transactionlimit" />
</label>
</p>
<p>Currency:-
<input type="text" name="currency" id="currency"value='<%=(request.getAttribute("currencyid")==null)?"":request.getAttribute("currencyid")%>'/>
</p>
<p>Daily Limit:-
<label>
<input type="text" name="dailylimit" id="dailylimit" value='<%=(request.getAttribute("dailylimit")==null)?"":request.getAttribute("dailylimit")%>'/>
</label>
</p>
<p>
<input type="submit"formaction="Connectioncheck" name="Update" id="Update" value="Update" />
</p>
<p> </p>
<hr />
Account List
<hr />
<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>
</tr>
<c:forEach var="user" items="${al}">
<tr>
<td><c:out value="${user}" /></td>
<td><c:out value="${user}" /></td>
<td><c:out value="${user}" /></td>
<td><c:out value="${user}" /></td>
</tr>
</c:forEach>
</table
</body>
</html>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.examples.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
*/
public class Search extends HttpServlet {
private String mobilenumber;
private String country;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Search</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Search at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int count =0;
PrintWriter out = response.getWriter();
TableElements tr = new TableElements();
try {
//processRequest(request, response);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = null;
PreparedStatement myStmt = null;
ArrayList al = new ArrayList();
String MiddleName;
String cardnumber;
String currencyid;
String bankaccntid;
String dailylimit = null;
String isspeciallimit;
conn = DriverManager.getConnection("jdbc:sqlserver://MPESA\\SQL2012;user=realm;password=friend;database=ElmaTest");
if(conn!=null)
{
out.println("Connection Succcesful");
myStmt = conn
.prepareStatement("exec p_GetAccountCustomer ?,?,?,?,?,?,?");
myStmt.setString(1, country);
myStmt.setString(2, "99");
myStmt.setString(3, mobilenumber);
myStmt.setString(4, "");
myStmt.setString(5, "0");
myStmt.setString(6, "2001");
myStmt.setString(7, "127.0.0.1");
ResultSet rs= myStmt.executeQuery();
while(rs.next())
{
count ++;
al.add(rs.getString("CardNumber"));
al.add(rs.getString("MiddleName"));
al.add(rs.getString("BankAccountID"));
al.add(rs.getString("CurrencyID"));
al.add(rs.getString("DayTransactionLimit"));
al.add(rs.getString("isspeciallimit"));
String country = rs.getString("CustomerCountry");
String customerid = rs.getString("CustomerID");
String TitleofAccount = rs.getString("TitleofAccount");
String FirstName = rs.getString("FirstName");
MiddleName = rs.getString("MiddleName");
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");
cardnumber = rs.getString("CardNumber");
bankaccntid = rs.getString("BankAccountID");
currencyid = rs.getString("CurrencyID");
isspeciallimit = rs.getString("IsSpecialLimit");
dailylimit = rs.getString("DayTransactionLimit");
System.out.println("Count" + count);
System.out.println(country);
System.out.println(customerid);
System.out.println(TitleofAccount);
System.out.println(FirstName);
System.out.println(MiddleName);
System.out.println(LastName);
System.out.println(City);
System.out.println(Address);
System.out.println(emailid);
System.out.println(typeofid);
System.out.println(Idnumber);
System.out.println(branchid);
System.out.println(cardnumber);
System.out.println(bankaccntid);
System.out.println(currencyid);
System.out.println(isspeciallimit);
request.setAttribute("count", count);
request.setAttribute("country", country);
request.setAttribute("customerid", customerid);
request.setAttribute("TitleofAccount", TitleofAccount);
request.setAttribute("FirstName", FirstName);
request.setAttribute("LastName", LastName);
request.setAttribute("City", City);
request.setAttribute("emailid", emailid);
request.setAttribute("typeofid", typeofid);
request.setAttribute("Idnumber", Idnumber);
request.setAttribute("branchid", branchid);
request.setAttribute("cardnumber", cardnumber);
request.setAttribute("bankaccntid", bankaccntid);
request.setAttribute("currencyid", currencyid);
request.setAttribute("isspeciallimit", isspeciallimit);
request.setAttribute("dailylimit", dailylimit);
}
System.out.println(al);
request.setAttribute("al",al);
RequestDispatcher rd = request.getRequestDispatcher("CustomerDetails.jsp");
rd.forward(request, response);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
PrintWriter out = response.getWriter();
mobilenumber = request.getParameter("mobilenumber");
country = request.getParameter("country");
doGet(request,response);
//out.println(mobilenumber);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
答案 0 :(得分:0)
在jsp中,您将列表中的每个项目打印4次
<c:forEach var="user" items="${al}">
<tr>
<td><c:out value="${user}" /></td>
<td><c:out value="${user}" /></td>
<td><c:out value="${user}" /></td>
<td><c:out value="${user}" /></td>
</tr>
</c:forEach>
您应该为列表中的每个项目设置一个TD,如下所示
<tr>
<c:forEach var="user" items="${al}">
<td><c:out value="${user}" /></td>
</c:forEach>
</tr>