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



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

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

但是数据不会被添加到数据库中,并显示这样的错误。我在下面写了完整的错误。

此URL不支持HTTP方法GETtype状态报告此URL不支持messageHTTP方法GETdescription请求的资源不允许使用指定的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>");
}
}
HttpServlet类有另一个方法doGet。您可以覆盖该方法,因此支持GET请求

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

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

此外,我看到你正试图映射你的客户发送的数据

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

使用以下对产品类别的对象

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

这将失败,因为您正在发送一系列产品。对于初学者,只需发送一个JSON对象,如以下

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

一旦您完成了这项工作,在为您的用例实现之前,请阅读任何GSON教程以获得更好的想法。一个教程可以在这里找到

附带说明,您不应该将conn、pst等声明为类成员变量,而是将它们放在方法中。此外,在try-and-catch之后,在最后一个块中关闭连接。

json字符串是数组,但解析为对象。因此,您需要解析为列表。关注:

@Test
public void testGson(){
String jsonStr = "[{"item":"Chocolate","pro_price":"32","qty":"1","total":"32"}, {"item":"Mango","pro_price":"10","qty":"1","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;
}

最新更新