我是Java的初学者。
我想要实现的是从JSP(index.jsp)网页输入产品代码,调用servlet(getDetails)并从DB2-I-series sedrver获取多个位置的描述和平衡
在Servlet中我是使用Map,但为了获得多个位置的平衡,我正在使用自定义类ArrayList(displayOnHand)。
在servlet页面中正确捕获数据以便在多个位置进行平衡,但是当我在Servlet中执行setAttribute并在JSP中执行相同的getAttribute时,我无法一次将数据提取为1个字段。
有人可以帮助男人。
以下是我的代码
-------- getDetails Servlet ------------------
public class getDetails extends HttpServlet {
/**
* 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. */
/* write your code here */
Map < String, String > messages = new HashMap < String, String > ();
ArrayList columnNames = new ArrayList();
displayOnHand data = new displayOnHand();
ArrayList < displayOnHand > dataList = new ArrayList < displayOnHand > ();
String scanSKU = request.getParameter("SKUScan");
if (scanSKU == null || scanSKU.trim().isEmpty()) {
messages.put("scanSKU", "Please Enter SKU/UPC/EAN Code");
}
if (messages.isEmpty()) {
String driver = getServletContext().getInitParameter("driver");
String connectionString = getServletContext().getInitParameter("connectionString");
String username = getServletContext().getInitParameter("username");
String password = getServletContext().getInitParameter("password");
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectionString, username, password);
Statement stmt = conn.createStatement();
String rsstmt = "select IDESCR from mm510lib.invmst where inumbr =" + scanSKU;
ResultSet rs = stmt.executeQuery(rsstmt);
String SKUName = "";
while (rs.next()) {
SKUName = rs.getString(1);
}
//String SKUName = rs.getString(1);
messages.put("SKUDescription", SKUName);
stmt = conn.createStatement();
rsstmt = "select istore, IBHAND from mm510lib.invbal where inumbr =" + scanSKU;
rs = stmt.executeQuery(rsstmt);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
//for (int i = 1; i <= columns; i++) {
// columnNames.add(md.getColumnName(i));
// }
// Get row data
//while (rs.next()) {
// ArrayList row = new ArrayList(columns);
// for (int i = 1; i <= columns; i++) {
// row.add(rs.getObject(i));
// }
// data.add(row);
//}
while (rs.next()) {
//displayOnHand data = new displayOnHand();
data.setLocationCode(rs.getInt(1));
data.setOnhand(rs.getInt(2));
dataList.add(data);
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(getDetails.class.getName()).log(Level.SEVERE, null, ex);
messages.put("connection", "Connection to Database could not be established");
}
request.setAttribute("messages", messages);
request.setAttribute("onHandClass", data);
request.setAttribute("OnHandDetails", dataList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}
---------------------------------------------------- index.jsp ----------------------------------------------------
<%@page import="java.util.List" %>
<%@page import="java.sql.Array" %>
<%@page import="java.util.Locale.Category" %>
<%@page import="java.util.ArrayList" %>
<%@page import="JavaPackages.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Store MIS V0.1</title>
<link href="css/index.css" rel="stylesheet" type="text/css" />
<script type='text/javascript'>
function isNumber(evt) {
evt = (evt) ? evt : window.event; // for IE
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
</script>
</head>
<body>
<div class="hypLogo">
<img src="images/logo.jpg" alt="" />
</div>
<div class="container">
<div class="pageHeader">
<h1>Store MIS</h1>
<form action="getDetails.do" method="POST">
<p>
<input type="text" name="SKUScan" value="" placeholder="Scan SKU" onkeypress="return isNumber(event)" />
</p>
<span class="error">${messages.scanSKU}</span>
<p class="submit">
<input type="submit" name="commit" value="Enter">
</p>
<span class="error">${messages.connection}</span>
<span class="error">${messages.SKUDescription}</span>
</form>
</div>
</div>
<% out.println( "in Script"); if (request.getAttribute( "OnHandDetails") !=n ull) { // retrieve your list from the request, with casting ArrayList<displayOnHand>list = (ArrayList
<displayOnHand>) request.getAttribute("OnHandDetails"); displayOnHand tempData = new displayOnHand() ; out.println("Array Size" + list.size()); // print the information about every category of the list for (int i = 0; i
< list.size(); i++) { tempData.equals(list.get(i));
} } %>
</body>
</html>
------------- displayOnHand Class ---------------
public class displayOnHand {
private int storeNumber;
private int onHandBal;
public displayOnHand() {
}
public void setLocationCode(int strNum) {
storeNumber = strNum;
}
public int getLocationCode() {
return storeNumber;
}
public void setOnhand(int onHand) {
onHandBal = onHand;
}
public int getOnHand() {
return onHandBal;
}
}
答案 0 :(得分:0)
您可以使用jstl标记库迭代arraylist。例如,您可以使用c:forEach
代码。
答案 1 :(得分:0)
对于迭代,您也可以使用for循环 e.g。
for(i=0;i<list.size();i++)
out.println(list.get(i));
OR
你也可以使用JvL标签作为@vivek提到
答案 2 :(得分:0)
你真的应该尝试使用JSTL。
就像(从JSTL c:forEach printing ${}被盗)一样简单:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title></title>
</head>
<body>
Hello from products page.<br/>
<c:forEach var="prod" items="${products}">
${prod.price}
${prod.id}
</c:forEach>
</body>
</html>
使用漂亮的XML文件。
如果你真的不能使用JSTL,那就是:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.util.List"%>
<%@page import="package.to.Product"%>
<html>
<head>
<title></title>
</head>
<body>
Hello from products page.<br/>
<%
List<Product> products = (List<Product>) request.getAttribute("products");
for (Product prod: products) {
page.setAttribute("prod", prod);
%>
${prod.price}
${prod.id}
<% } %>
</body>
</html>
非常丑陋不是它(我甚至没有测试过它)?相信我:除非你真的不能做不同的避免 scriptlets!
答案 3 :(得分:0)
您可以在列表中添加产品实例,例如
List<Product> productList = new ArrayList<Product>();
并将产品添加为productList.add(p);
,其中p是Product的任何实例。用于循环的显示器,例如
for(i=0;i<productList.size();i++)
{
Product product = list.get(i);
out.println(product.price);
out.println(product.id);
}