java - Servlet 更新失败并返回空白页
问题描述
一般来说,我对编程很陌生,我在我的 Java Web 应用程序中遇到了某个问题,尽管从 MySQL 数据库中添加和删除产品工作正常,但我的产品信息没有更新。当我在更新页面上点击提交时,程序只会返回一个空白页面,并且 MySQL 数据库中的值不会更新。
Netbeans 日志和 Apache tomcat 日志没有返回错误,所以我只能认为重定向将转到一个不存在的空白页面,但我尝试在表单操作中添加“../”,但它不起作用。
HTML表单代码:
<form method="post" action="product.update">
<div class="row gtr-uniform">
<!-- hidden form -->
<input type="hidden" name="id" value="${product.getID()}">
<div class="col-6 col-12-xsmall">
<input type="text" id="name" placeholder="Product name" value="${product.getName()}" required />
</div>
<div class="col-6 col-12-xsmall">
<input type="text" id="quantity" placeholder="Available quantity" value="${product.getProductQuantity()}" required />
</div>
<div class="col-6 col-12-xsmall">
<input type="text" id="price" placeholder="Product price" value="${product.getPrice()}" required />
</div>
<!-- Break -->
<div class="col-12">
<ul class="actions">
<li><input type="submit" value="Submit Form" class="primary" /></li>
</ul>
</div>
</div>
</form>
更新产品 Servlet:
response.setContentType("text/html;charset=UTF-8");
String str_id=request.getParameter("id");
String name=request.getParameter("name");
String str_price=request.getParameter("price");
String str_quantity=request.getParameter("quantity");
ArrayList<String> errormsg=new ArrayList<>();
boolean success=true;
if(str_id==null || name==null || str_price==null || str_quantity==null){
errormsg.add("Error: Null parameters.");
success=false;
}
else if(str_id.isEmpty() || name.isEmpty() || str_price.isEmpty() || str_quantity.isEmpty()){
errormsg.add("Error: Empty parameters.");
success=false;
}
else{
try{
int id=Integer.parseInt(str_id);
ProductDAO func=new ProductDAO();
ProductBean bean=func.getProductByID(id);
Double price=Double.parseDouble(str_price);
int quantity=Integer.parseInt(str_quantity);
success=func.updatePrice(bean, price) && func.updateName(bean, name) && func.updateStock(bean, quantity);
}
catch(NumberFormatException e){
success=false;
errormsg.add("Error: IDs are not integers.");
}
RequestDispatcher rd=request.getRequestDispatcher("WEB-INF/product/productupdate_success.jsp");
if(success==false){
rd=request.getRequestDispatcher("WEB-INF/errors/error.jsp");
request.setAttribute("error", errormsg);
}
rd.forward(request, response);
}
产品豆类:
public class ProductBean implements java.io.Serializable{
private int id;
private Double price;
private String name;
private int quantity;
public ProductBean(){
this.id=-1;
this.price=0.0;
this.name="empty_noitem";
this.quantity=0;
}
public ProductBean(int id, double prc, String nm, int qty){
this.id=id;
this.price=prc;
this.name=nm;
this.quantity=qty;
}
// setters
public void updateID(int id){
this.id=id;
}
public void updatePrice(double prc){
this.price=prc;
}
public void updateName(String nm){
this.name=nm;
}
public void updateProductQuantity(int qty){
this.quantity=qty;
}
// getters
public int getID(){
return this.id;
}
public double getPrice(){
return this.price;
}
public String getName(){
return this.name;
}
public int getProductQuantity(){
return this.quantity;
}
}
产品DAO类:
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.ResourceBundle;
/**
*
* @author SpicyAsh
*/
public class ProductDAO implements java.io.Serializable{
private String driver;
private String url;
private String user;
private String pass;
private boolean loadClass(){
try{
Class.forName(driver);
}catch (ClassNotFoundException e){
e.printStackTrace();
return false;
}
return true;
}
public void loadSQL(){
ResourceBundle sql=ResourceBundle.getBundle("system.sql.sql");
this.url=sql.getString("url");
this.user=sql.getString("user");
this.pass=sql.getString("password");
this.driver=sql.getString("driver");
}
public ProductDAO(String driver, String url, String user, String pass)
{
this.url=url;
this.user=user;
this.pass=pass;
this.driver=driver;
}
public ProductDAO()
{
loadSQL();
}
public boolean exists(ProductBean bean){
if(getProductByID(bean.getID())!=null || getProductByName(bean.getName())!=null){
return true;
}
// else
return false;
}
public boolean addProduct(ProductBean bean){
loadClass();
String sql="insert into product(product_name, price, quantity) values(?,?,?)";
boolean success=true;
// writing try-catch like this automatically closes the resources
// this is called try-with-resources
try(Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setString(1, bean.getName());
ps.setDouble(2, bean.getPrice());
ps.setInt(3, bean.getProductQuantity());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
}
return success;
}
public ProductBean getProductByID(int inid){
loadClass();
String sql="select * from product where ID=?";
ProductBean prod=new ProductBean();
try(Connection conn=DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);){
ps.setInt(1, inid);
ResultSet rs=ps.executeQuery();
while(rs.next()){
int id=rs.getInt("ID");
String name=rs.getString("product_name");
double price=rs.getDouble("price");
int quantity=rs.getInt("quantity");
prod.updateID(id);
prod.updateName(name);
prod.updatePrice(price);
prod.updateProductQuantity(quantity);
}
}
catch(SQLException e){
e.printStackTrace();
}
return prod;
}
public ProductBean getProductByName(String inname){
loadClass();
String sql="select * from product where product_name=?";
ProductBean prod=new ProductBean();
try(Connection conn=DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);){
ps.setString(1, inname);
ResultSet rs=ps.executeQuery();
while(rs.next()){
int id=rs.getInt("ID");
String name=rs.getString("product_name");
double price=rs.getDouble("price");
int quantity=rs.getInt("quantity");
prod.updateID(id);
prod.updateName(name);
prod.updatePrice(price);
prod.updateProductQuantity(quantity);
}
}
catch(SQLException e){
e.printStackTrace();
}
return prod;
}
public ArrayList<ProductBean> getAllProducts(){
loadClass();
String sql="select * from product";
ProductBean bean=new ProductBean();
ArrayList <ProductBean> arrList=new ArrayList<ProductBean>();
try(Connection conn=DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);){
ResultSet rs=ps.executeQuery();
while(rs.next()){
bean=new ProductBean();
int id=rs.getInt("ID");
String name=rs.getString("product_name");
double price=rs.getDouble("price");
int quantity=rs.getInt("quantity");
bean.updateID(id);
bean.updateName(name);
bean.updatePrice(price);
bean.updateProductQuantity(quantity);
arrList.add(bean);
}
}
catch(SQLException e){
e.printStackTrace();
}
return arrList;
}
// update methods
public boolean updatePrice(ProductBean row, Double newPrice){
if(!exists(row)){
return false;
}
loadClass();
boolean success=true;
String sql="update product set price=? where id=?";
try(Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setDouble(1, newPrice);
ps.setInt(2, row.getID());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
e.printStackTrace();
}
return success;
}
// update methods
public boolean updateName(ProductBean row, String newName){
if(!exists(row)){
return false;
}
loadClass();
boolean success=true;
String sql="update product set product_name=? where id=?";
try(Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setString(1, newName);
ps.setInt(2, row.getID());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
e.printStackTrace();
}
return success;
}
public boolean updateStock(ProductBean row, int newStock){
if(!exists(row)){
return false;
}
loadClass();
boolean success=true;
String sql="update product set quantity=? where id=?";
try(Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setInt(1, newStock);
ps.setInt(2, row.getID());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
e.printStackTrace();
}
return success;
}
// delete method (ONLY ADMIN/OWNDER)
public boolean removeProduct(ProductBean row){
if(!exists(row)){
return false;
}
loadClass();
boolean success=true;
String sql="delete from product where id=?";
try(Connection conn=DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setInt(1,row.getID());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
e.printStackTrace();
}
return success;
}
}
MySQL中的产品表结构:
场地 | 类型 | 无效的 | 钥匙 | 默认 | 额外的 |
---|---|---|---|---|---|
ID | 整数 | 不 | PRI | 无效的 | 自动递增 |
产品名称 | varchar(50) | 是的 | 无效的 | ||
价格 | 双倍的 | 是的 | 无效的 | ||
数量 | 整数 | 是的 | 0 |
解决方案
您应该考虑将 MVC Web 应用程序迁移到 Spring BOOT,然后将 Spring Controllers 和 ThymeLeaf 用于视图。比使用旧的 Servlet 和 JSP 要好得多。从这里开始:
https://dzone.com/articles/introduction-to-thymeleaf-in-spring-boot
推荐阅读
- flutter - 在 for 循环内进行 bloc 调用后颤动读取流
- pandas - 尝试就地使用列表 Pandas
- python - 如何理解这个递归算法
- laravel - 如何在 laravel 分页链接中附加 csrf_field()?
- numpy - 尝试导入 tensorflow_hub 时出现此错误:无法从“tensorflow.python.distribute”导入名称“parameter_server_strategy_v2”
- python - 如果在 sqlite3 python 中不存在,则使用变量创建一个表
- css - 较大标签尺寸的上半部分在 react-native-simple-radio-button 中被切断
- c# - EF Core:如何以编程方式访问 DBSet 以用于种子目的
- javascript - 如何使用异步功能访问天气 API 降雨数据
- graphql - 在单个集合中保留多种类型(嵌套类型/嵌入类型):