我正在使用Jsp Ajax创建一个简单销售系统。当我添加数据时,单击添加按钮,所有HTML表数据已成功传递到我在控制台上看到的salesadd Servlet页面 我在Servlet Page中接收了这种格式的数据
[{“ item”:“巧克力”,“ pro_price”:“ 32”,“数量”:“ 1”,“总计”:“ 32”}, {“ item”:“ Mango”,“ pro_price”:“ 10”,“ qty”:“ 1”,“ total”:“ 10”}]
但是数据未添加到数据库中,并显示这样的错误。我在下面写了“完全错误”。
此URL不支持HTTP方法GET 类型状态报告 此URL不支持messageHTTP方法GET description所请求的资源不允许使用指定的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>");
}