对于POST请求,Java Servlet返回错误405(方法不允许)

编程入门 行业动态 更新时间:2024-10-09 02:31:13
本文介绍了对于POST请求,Java Servlet返回错误405(方法不允许)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的服务对于获取请求很好,但是当我调用POST(使用jquery ajax $ .post)时,我收到错误405(方法不允许)

My servet work fine for get requests but when I call POST (using jquery ajax $.post) I get error 405 (Method Not Allowed)

这是我的代码:

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class init extends HttpServlet { public init() { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("GET"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IllegalStateException { response.setContentType("application/json"); ServletInputStream in = request.getInputStream(); PrintWriter out = response.getWriter(); out.print("POST"); } }

推荐答案

是你确定你实际上覆盖了doPost方法吗?

Are you sure that you actually override the doPost Method?

签名如下所示:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException

但是你这样指定:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IllegalStateException

我不确定是否允许这样做: 使用新检查的异常覆盖Java接口

I am not sure whether this is allowed: Overriding Java interfaces with new checked exceptions

请尝试以下步骤:

  • 添加@Override注释
  • 删除抛出IllegalStateExceptio n(应该不是问题,因为它是运行时异常)
  • 更多推荐

    对于POST请求,Java Servlet返回错误405(方法不允许)

    本文发布于:2023-10-10 18:31:20,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1479385.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:不允许   错误   方法   Java   POST

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!