Servlet错误:HTTP状态405 – 此URL不支持HTTP方法GET。

huangapple go评论69阅读模式
英文:

Servlet error : HTTP Status 405 - HTTP method GET is not supported by this URL

问题

以下是翻译好的内容:

当我尝试在Eclipse中运行代码时,它不会传递参数。
请检查下面的图像网址。

Servlet错误:HTTP状态405 – 此URL不支持HTTP方法GET。

但是当我单独运行HTML代码时,它会传递值。

Servlet错误:HTTP状态405 – 此URL不支持HTTP方法GET。

我不知道为什么会这样。
我的Servlet代码是

package com.example;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddServelet extends HttpServlet
{
	
	public void services(HttpServletRequest req, HttpServletResponse resp) {

		int i = Integer.parseInt(req.getParameter("num1"));
		int j = Integer.parseInt(req.getParameter("num2"));
		
		int k = i+j;	
		System.out.println(k);	
	}
}

我的web.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>abc</servlet-name>
<servlet-class>com.example.AddServelet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>

</web-app>

我的HTML代码是

<html>
<body>
<form action="add">
Number 1: <input type="number" name="num1"><br>
Number 2: <input type="number" name="num2"><br>
<input type="submit">
</form>
</body>
</html>

有人能帮助我解决这个问题吗?
提前谢谢您。

英文:

When i try to run the code in eclipse it won't pass the arguments.
please check the below images url.

Servlet错误:HTTP状态405 – 此URL不支持HTTP方法GET。

But when i individually run the html code it passes the values.

Servlet错误:HTTP状态405 – 此URL不支持HTTP方法GET。

I don't know why this work like this.
My Servlet code is

package com.example;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddServelet extends HttpServlet
{
	
	public void services(HttpServletRequest req, HttpServletResponse resp) {

		int i = Integer.parseInt(req.getParameter(&quot;num1&quot;));
		int j = Integer.parseInt(req.getParameter(&quot;num2&quot;));
		
		int k = i+j;	
		System.out.println(k);	
	}
}

and my web.xml code is

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;4.0&quot;&gt;

&lt;servlet&gt;
&lt;servlet-name&gt;abc&lt;/servlet-name&gt;
&lt;servlet-class&gt;com.example.AddServelet&lt;/servlet-class&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
&lt;servlet-name&gt;abc&lt;/servlet-name&gt;
&lt;url-pattern&gt;/add&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

&lt;/web-app&gt;

my html code

&lt;html&gt;
&lt;body&gt;
&lt;form action=&quot;add&quot;&gt;
Number 1: &lt;input type=&quot;number&quot; name=&quot;num1&quot;&gt;&lt;br&gt;
Number 2: &lt;input type=&quot;number&quot; name=&quot;num2&quot;&gt;&lt;br&gt;
&lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Can anyone please help me with this.
Thank you in advance.

答案1

得分: 2

你需要重写doPost方法,因为你正在提交表单,而这是一个POST请求。

public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int i = Integer.parseInt(request.getParameter("num1"));
        int j = Integer.parseInt(request.getParameter("num2"));

        int k = i + j;    
        System.out.println(k); 
    }
}
英文:

You need to override the doPost method because you are submitting the form and it's a post request.

public class AddServelet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public AddServelet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
	
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
	int i = Integer.parseInt(req.getParameter(&quot;num1&quot;));
    int j = Integer.parseInt(req.getParameter(&quot;num2&quot;));

    int k = i+j;    
    System.out.println(k); 
}

}

答案2

得分: 1

覆盖 HttpServletprotected void doGet(HttpServletRequest req, HttpServletResponse resp) 方法,如果你想处理 GET 请求。

英文:

override the HttpServlet protected void doGet(HttpServletRequest req, HttpServletResponse resp) method if you want to handle a GET request

huangapple
  • 本文由 发表于 2020年4月4日 03:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61018802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定