gpt4 book ai didi

java - 此 URL 类型 Servlet 错误不支持 HTTP 方法 GET

转载 作者:行者123 更新时间:2023-12-01 22:00:32 24 4
gpt4 key购买 nike

我正在使用 Jsp Ajax 创建一个简单的销售系统。当我添加数据时,单击“添加”按钮,所有 Html 表数据均成功传递到我在控制台上看到的 salesadd Servlet 页面我接收的数据看起来像这样格式到 Servlet 页面

[{"item":"巧克力","pro_price":"32","qty":"1","total":"32"},{"item":"Mango","pro_price":"10","qty":"1","total":"10"}]

但是数据没有添加到数据库并显示这样的错误。我在下面写了完整的错误。

此 URL 不支持 HTTP 方法 GET类型 状态报告此 URL 不支持 messageHTTP 方法 GET说明请求的资源不允许指定的HTTP方法。

我尝试过,所以现在我附上了下面的内容。我认为可能是产品类别的问题

Product.java

public class Product
{
private String item;
private int price;
private int qty;
private int total;
public String getItem()
{
return item;
}
public void setItem(String item)
{
this.item = item;
}
public int getPrice()
{
return price;
}

public void setPrice(int price)
{
this.price = price;
}
public int getQty()
{
return qty;
}
public void setQty(int qty)
{
this.qty = qty;
}

public int getTotal()
{
return total;
}

public void setTotal(int total)
{
this.total = total;
}
}

Servlet 页面

salesadd.java

@WebServlet("/salesadd")
public class salesadd extends HttpServlet {
Connection con;
PreparedStatement pst;
int row;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);

Connection con;
PreparedStatement pst;
String jsonData = request.getParameter("data1");
PrintWriter out = response.getWriter();

Gson gson = new Gson();
Product data1 = gson.fromJson(jsonData, Product.class);
String item = data1.getItem();
int price = data1.getPrice();
int qty = data1.getQty();
int total = data1.getTotal();




try
{
con = DriverManager.getConnection("jdbc:mysql://localhost/icepos", "root", "");
pst = con.prepareStatement("insert into sale_product(item,price,qty,total)values(?,?,?,?) ");
pst.setString(1, data1.getItem());
pst.setInt(2, data1.getPrice());
pst.setInt(3, data1.getQty());
pst.setInt(4, data1.getTotal());
pst.executeUpdate();

out.println("<font color='green'> Record Adddd </font>");
} catch (SQLException ex) {
out.println("<font color='red'> Record Failed </font>");
}

}







public void doPost(HttpServletRequest req,HttpServletResponse rsp ) throws IOException,ServletException
{
rsp.setContentType("text/html");
PrintWriter out = rsp.getWriter();


out.println("<font color='green'> Record Adddd </font>");

}

最佳答案

您可以循环列表,如下所示:

for (Product product : objectList) {
try
{
con = DriverManager.getConnection("jdbc:mysql://localhost/icepos", "root", "");
pst = con.prepareStatement("insert into sale_product(item,price,qty,total)values(?,?,?,?) ");
pst.setString(1, product.getItem());
pst.setInt(2, product.getPrice());
pst.setInt(3, product.getQty());
pst.setInt(4, product.getTotal());
pst.executeUpdate();

out.println("<font color='green'> Record Adddd </font>");
} catch (SQLException ex) {
out.println("<font color='red'> Record Failed </font>");
}
}

关于java - 此 URL 类型 Servlet 错误不支持 HTTP 方法 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com