运行.jsp
文件时,出现错误:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [57] in the jsp file: [/index.jsp]
The method getAllProducts() from the type ProductDaoImpl refers to the missing type ProductBean
54:
55: List<ProductBean> products = new ArrayList<ProductBean>();
56:
57: products = prodDao.getAllProducts();
58:
59: for(ProductBean product : products){
60:
这是我的index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="adrian.dao.*,adrian.beans.*,java.util.*,javax.servlet.ServletOutputStream,java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Ellison Electronics</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="css/changes.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<%
/* Checking the user credentials */
String userName = (String)session.getAttribute("username");
String password = (String)session.getAttribute("password");
String userType = (String)session.getAttribute("usertype");
boolean isValidUser = true;
if(userType == null || userName == null || password==null || !userType.equals("customer")){
isValidUser = false;
}
%>
<%
if(isValidUser) {
%>
<%@ include file="userHeader.html" %>
<%
}
else{
%>
<%@ include file="header.html" %>
<%
}
%>
<!-- Start of Product Items List -->
<div class="products" style="background-color: #E6F9E6;">
<div class="row text-center" >
<%
ProductDaoImpl prodDao = new ProductDaoImpl();
List<ProductBean> products = new ArrayList<ProductBean>(); <----------- here is my error
products = prodDao.getAllProducts();
for(ProductBean product : products){
String addToCartUrl = null;
String buyNowUrl = null;
if(isValidUser){
addToCartUrl = "./AddtoCart?uid="+userName+"&pid="+product.getProdId()+"";
buyNowUrl = "./BuyNow?uid="+userName+"&pid="+product.getProdId()+"";
}
else{
addToCartUrl = "login.html";
buyNowUrl = "login.html";
}
%>
<div class="col-sm-4">
<div class="thumbnail">
<img src="./ShowImage?pid=<%=product.getProdId() %>" alt="Product" style="height:200px; max-width:200px; max-width:300px;">
<p class="productname"><%=product.getProdName() %> ( <%=product.getProdId() %> ) </p>
<p class="productinfo"><%=product.getProdInfo() %></p>
<p class="price">Rs <%=product.getProdPrice() %> </p>
<form method="post">
<button type="submit" formaction="<%= addToCartUrl%>">Add to Cart</button>
<button type="submit" formaction="<%= buyNowUrl%>">Buy Now</button>
</form>
</div>
</div>
<%
}
%>
</div>
</div>
<!-- ENd of Product Items List -->
<%@ include file="footer.html" %>
</body>
</html>
ProductDaoImpl.java
package adrian.dao;
import java.io.InputStream;
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.List;
import adrian.beans.ProductBean;
import adrian.beans.DemandBean;
import adrian.utility.DBUtil;
import adrian.utility.IDUtil;
import adrian.utility.MailMessage;
public class ProductDaoImpl implements ProductDao{
@Override
public String addProduct(String prodName, String prodType, String prodInfo, double prodPrice, int prodQuantity,
InputStream prodImage) {
String status = null;
String prodId = IDUtil.generateId();
ProductBean product = new ProductBean(prodId, prodName, prodType, prodInfo, prodPrice, prodQuantity, prodImage);
status = addProduct(product);
return status;
}
@Override
public String addProduct(ProductBean product) {
String status = "Product Registration Failed!";
if(product.getProdId()==null)
product.setProdId(IDUtil.generateId());
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
try {
ps = con.prepareStatement("insert into product values(?,?,?,?,?,?,?);");
ps.setString(1, product.getProdId());
ps.setString(2, product.getProdName());
ps.setString(3, product.getProdType());
ps.setString(4, product.getProdInfo());
ps.setDouble(5, product.getProdPrice());
ps.setInt(6,product.getProdQuantity());
ps.setBlob(7, product.getProdImage());
int k = ps.executeUpdate();
if(k>0) {
status = "Product Added Successfully with Product Id: "+product.getProdId();
}
else {
status = "Product Updation Failed!";
}
} catch (SQLException e) {
status = "Error: "+e.getMessage();
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
return status;
}
@Override
public String removeProduct(String prodId) {
String status = "Product Removal Failed!";
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
PreparedStatement ps2 = null;
try {
ps = con.prepareStatement("delete from product where pid=?");
ps.setString(1, prodId);
int k = ps.executeUpdate();
if(k>0) {
status = "Product Removed Successfully!";
ps2 = con.prepareStatement("delete from usercart where prodid=?");
ps2.setString(1, prodId);
ps2.executeUpdate();
}
} catch (SQLException e) {
status = "Error: "+e.getMessage();
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
DBUtil.closeConnection(ps2);
return status;
}
@Override
public String updateProduct(ProductBean prevProduct, ProductBean updatedProduct) {
String status = "Product Updation Failed!";
if(!prevProduct.getProdId().equals(updatedProduct.getProdId())) {
status = "Both Products are Different, Updation Failed!";
return status;
}
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
try {
ps = con.prepareStatement("update product set pname=?,ptype=?,pinfo=?,pprice=?,pquantity=?,image=? where pid=?");
ps.setString(1, updatedProduct.getProdName());
ps.setString(2, updatedProduct.getProdType());
ps.setString(3, updatedProduct.getProdInfo());
ps.setDouble(4, updatedProduct.getProdPrice());
ps.setInt(5, updatedProduct.getProdQuantity());
ps.setBlob(6, updatedProduct.getProdImage());
ps.setString(7, prevProduct.getProdId());
int k = ps.executeUpdate();
if(k>0)
status = "Product Updated Successfully!";
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
return status;
}
@Override
public String updateProductPrice(String prodId, double updatedPrice) {
String status = "Price Updation Failed!";
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
try {
ps = con.prepareStatement("update product set pprice=? where pid=?");
ps.setDouble(1, updatedPrice);
ps.setString(2, prodId);
int k = ps.executeUpdate();
if(k>0)
status = "Price Updated Successfully!";
} catch (SQLException e) {
status = "Error: "+e.getMessage();
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
return status;
}
@Override
public List<ProductBean> getAllProducts() {
List<ProductBean> products = new ArrayList<ProductBean>();
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from product");
rs = ps.executeQuery();
while(rs.next()) {
ProductBean product = new ProductBean();
product.setProdId(rs.getString(1));
product.setProdName(rs.getString(2));
product.setProdType(rs.getString(3));
product.setProdInfo(rs.getString(4));
product.setProdPrice(rs.getDouble(5));
product.setProdQuantity(rs.getInt(6));
product.setProdImage(rs.getAsciiStream(7));
products.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
DBUtil.closeConnection(rs);
return products;
}
@Override
public byte[] getImage(String prodId) {
byte[] image = null;
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select image from product where pid=?");
ps.setString(1, prodId);
rs = ps.executeQuery();
if(rs.next())
image = rs.getBytes("image");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
DBUtil.closeConnection(rs);
return image;
}
@Override
public ProductBean getProductDetails(String prodId) {
ProductBean product = null;
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from product where pid=?");
ps.setString(1, prodId);
rs = ps.executeQuery();
if(rs.next()) {
product = new ProductBean();
product.setProdId(rs.getString(1));
product.setProdName(rs.getString(2));
product.setProdType(rs.getString(3));
product.setProdInfo(rs.getString(4));
product.setProdPrice(rs.getDouble(5));
product.setProdQuantity(rs.getInt(6));
product.setProdImage(rs.getAsciiStream(7));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
return product;
}
@Override
public String updateProductWithoutImage(String prevProductId, ProductBean updatedProduct) {
String status = "Product Updation Failed!";
if(!prevProductId.equals(updatedProduct.getProdId())) {
status = "Both Products are Different, Updation Failed!";
return status;
}
/*System.out.println("pId: "+updatedProduct.getProdId());
System.out.println("pName: "+updatedProduct.getProdName());
System.out.println("pType: "+updatedProduct.getProdType());
System.out.println("pInfo: "+updatedProduct.getProdInfo());
System.out.println("pPrice: "+updatedProduct.getProdPrice());
System.out.println("pQuantity: "+updatedProduct.getProdQuantity());*/
int prevQuantity = new ProductDaoImpl().getProductQuantity(prevProductId);
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
try {
ps = con.prepareStatement("update product set pname=?,ptype=?,pinfo=?,pprice=?,pquantity=? where pid=?");
ps.setString(1, updatedProduct.getProdName());
ps.setString(2, updatedProduct.getProdType());
ps.setString(3, updatedProduct.getProdInfo());
ps.setDouble(4, updatedProduct.getProdPrice());
ps.setInt(5, updatedProduct.getProdQuantity());
ps.setString(6, prevProductId);
int k = ps.executeUpdate();
//System.out.println("prevQuantity: "+prevQuantity);
if((k>0) && (prevQuantity < updatedProduct.getProdQuantity())) {
status = "Product Updated Successfully!";
//System.out.println("updated!");
List<DemandBean> demandList = new DemandDaoImpl().haveDemanded(prevProductId);
for(DemandBean demand : demandList) {
String userFName = new UserDaoImpl().getFName(demand.getUserName());
MailMessage.productAvailableNow(demand.getUserName(), userFName, updatedProduct.getProdName(), prevProductId);
boolean flag = new DemandDaoImpl().removeProduct(demand.getUserName(), prevProductId);
if(flag)
status += " And Mail Send to the customers who were waiting for this product!";
}
}
else if(k>0)
status = "Product Updated Successfully!";
else
status = "Product Not available in the store!";
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
//System.out.println("Prod Update status : "+status);
return status;
}
@Override
public double getProductPrice(String prodId) {
double price = 0;
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from product where pid=?");
ps.setString(1, prodId);
rs = ps.executeQuery();
if(rs.next()) {
price = rs.getDouble("pprice");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
return price;
}
@Override
public boolean sellNProduct(String prodId,int n) {
boolean flag = false;
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
try {
ps = con.prepareStatement("update product set pquantity=(pquantity - ?) where pid=?");
ps.setInt(1, n);
ps.setString(2, prodId);
int k = ps.executeUpdate();
if(k>0)
flag = true;
} catch (SQLException e) {
flag = false;
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
return flag;
}
@Override
public int getProductQuantity(String prodId) {
int quantity=0;
Connection con = DBUtil.provideConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from product where pid=?");
ps.setString(1, prodId);
rs = ps.executeQuery();
if(rs.next()) {
quantity = rs.getInt("pquantity");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
return quantity;
}
}
ProductBean.java
package adrian.beans;
import java.io.InputStream;
import java.io.Serializable;
public class ProductBean implements Serializable{
public ProductBean() {}
private String prodId;
private String prodName;
private String prodType;
private String prodInfo;
private double prodPrice;
private int prodQuantity;
private InputStream prodImage;
public ProductBean(String prodId, String prodName, String prodType, String prodInfo, double prodPrice,
int prodQuantity, InputStream prodImage) {
super();
this.prodId = prodId;
this.prodName = prodName;
this.prodType = prodType;
this.prodInfo = prodInfo;
this.prodPrice = prodPrice;
this.prodQuantity = prodQuantity;
this.prodImage = prodImage;
}
public String getProdId() {
return prodId;
}
public void setProdId(String prodId) {
this.prodId = prodId;
}
public String getProdName() {
return prodName;
}
public void setProdName(String prodName) {
this.prodName = prodName;
}
public String getProdType() {
return prodType;
}
public void setProdType(String prodType) {
this.prodType = prodType;
}
public String getProdInfo() {
return prodInfo;
}
public void setProdInfo(String prodInfo) {
this.prodInfo = prodInfo;
}
public double getProdPrice() {
return prodPrice;
}
public void setProdPrice(double prodPrice) {
this.prodPrice = prodPrice;
}
public int getProdQuantity() {
return prodQuantity;
}
public void setProdQuantity(int prodQuantity) {
this.prodQuantity = prodQuantity;
}
public InputStream getProdImage() {
return prodImage;
}
public void setProdImage(InputStream prodImage) {
this.prodImage = prodImage;
}
}
有人可以帮助我解决这个错误吗?