java - 此 URL 类型不支持 HTTP 方法 GET Servlet 错误
问题描述
我正在使用 Jsp Ajax 创建一个简单的销售系统。当我添加数据单击添加按钮时,所有 Html 表数据都成功传递到 salesadd Servlet 页面我在控制台上看到我收到的数据看起来像这种格式到 Servlet 页面
[{"item":"Chocolate","pro_price":"32","qty":"1","total":"32"}, {"item":"Mango","pro_price":"10 ","数量":"1","总计":"10"}]
但是数据没有添加到数据库中并显示这样的错误。我在下面写了完整的错误。
此 URL 类型不支持 HTTP 方法 GET 状态报告消息此 URL 描述不支持 HTTP 方法 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;
}
}
小服务程序页面
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>");
}
解决方案
HttpServlet 类有另一个方法doGet。可以覆盖方法,所以支持GET请求
推荐阅读
- php - S3 PHP putObject 失败并出现 403,可能是错误的文件名
- php - Apache2 每 1 分 3 秒重启一次 - Ubuntu 16.04
- angular - 在页面加载Angular 5,ODATA V3时逐渐加载数据
- r - 如何在时间序列数据中用 R 表示“IFELSE”?
- javascript - 在 Node.js 中创建网站正常运行时间监视器
- dart - 如何在我的 Flutter 应用程序中重置基本路由 - 即弹出任何路由并使用 Navigator 将其替换为新路由
- html - 使用 Flexbox 的侧边栏:包含的链接没有正确展开
- python - 如何刮取投手的姓名和球队?
- amazon-web-services - 在 AppSync 架构中检索嵌套信息数组
- ios - 滑动动画和功能