有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Getting错误“此URL不支持消息HTTP方法GET”,但从未使用GET anywhere

网络。xml文件:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <servlet> <servlet-name>adding</servlet-name> <servlet-class>com.nokia.web.servlet.practice.AddServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>adding</servlet-name> <url-pattern>/add</url-pattern> </servlet-mapping> </web-app>

索引。html文件:

<!DOCTYPE html> <html> <body> Hello World <form action="add" method="post"> Enter 1st number : <input type="text" name="num1"> Enter 2nd number : <input type="text" name="num2"> <input type="submit"> </form> </body> </html>

AddServlet。java文件:

package com.nokia.web.servlet.practice; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.*; public class AddServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { int i = Integer.parseInt(req.getParameter("num1")); int j = Integer.parseInt(req.getParameter("num2")); PrintWriter out = res.getWriter(); out.println(" post sum = "+ (i+j)); } }

错误是

HTTP Status 405 – Method Not Allowed Type Status Report Message HTTP method GET is not supported by this URL Description The method received in the request-line is known by the origin server but not supported by the target resource.

请给出一些建议如何使它工作。 ......................................................................................................................................................................................

Tags:  

共 (1) 个答案

  1. # 1 楼答案

    是否要将URL设置为localhost:8085/WebProjectYutb/add?num1=11&;提交后num2=11,这意味着它正在使用GET方法,因为POST方法在URL处不显示参数。 由于您没有实现GET方法,所以您正在接收HTTP状态405–方法不允许

    您可以通过覆盖doGet方法并将请求转发到doPost来解决此问题,如下所示:

    package com.nokia.web.servlet.practice;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class AddServlet extends HttpServlet {
    
        private static final long serialVersionUID = 7041461467204399253L;
    
        public AddServlet() {
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            doPost(req, resp);
        }
    
        @Override
        public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
            int i = Integer.parseInt(req.getParameter("num1"));
            int j = Integer.parseInt(req.getParameter("num2"));
    
            PrintWriter out = res.getWriter();
            out.println(" post sum = " + (i + j));
    
        }
    }