首页 > 解决方案 > 此 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>");

}

标签: javajsp

解决方案


HttpServlet 类有另一个方法doGet。可以覆盖方法,所以支持GET请求


推荐阅读