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

我正在使用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>");

}
avril_li 回答:此URL类型不支持HTTP方法GET Servlet错误

HttpServlet类具有另一个方法doGet。 您可以覆盖该方法,因此支持GET请求

,

您的客户端(来自浏览器)正在发出GET请求,并且由于您没有doGet()方法,因此您会收到此错误。

尽管您可以在类中添加doGet()方法,但这不能解决您的用例问题。您需要从客户端发送POST请求(在客户端检查您的AJAX代码)。 GET请求不应具有正文,而应仅具有带有查询参数的请求URL。

此外,我看到您正在尝试映射客户端发送的数据

[{"item":"Chocolate","pro_price":"32","qty":"1","total":"32"},{"item":"Mango","pro_price":"10","total":"10"}]

使用以下内容到产品类的对象

Product data1 = gson.fromJson(jsonData,Product.class);

这将失败,因为您要发送一系列产品。对于初学者来说,只需发送一个JSON对象(如下所示)

{"item":"Chocolate","price":"32","total":"32"}

完成这项工作后,在为您的用例实现之前,请阅读任何GSON教程以获取更好的主意。可以找到一个教程here

作为补充说明,您不应将conn,pst等声明为类成员变量,而应将它们放在方法中。同样,在尝试并捕获后,在最后一个块中关闭连接。

,

您的json字符串是数组,但是您解析为一个对象。因此,您需要解析为一个列表。追踪:

@Test
public void testGson(){
    String jsonStr = "[{\"item\":\"Chocolate\",\"pro_price\":\"32\",\"qty\":\"1\",\"total\":\"32\"},{\"item\":\"Mango\",\"pro_price\":\"10\",\"total\":\"10\"}]";
    List<Product> objectList = getObjectList(jsonStr,Product.class);
    System.out.println(objectList);
}
public static <T> List<T> getObjectList(String jsonString,Class<T> cls){
    List<T> list = new ArrayList<>();
    try {
        Gson gson = new Gson();
        JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray();
        for (JsonElement jsonElement : arry) {
            list.add(gson.fromJson(jsonElement,cls));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;
}
,

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

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>");
        }
    }
本文链接:https://www.f2er.com/3160346.html

大家都在问