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

时间:2020-04-03 19:04:21

标签: java eclipse servlets runtime-error http-error

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

enter image description here

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

enter image description here

我不知道为什么这样工作。 我的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>

有人可以帮我吗? 预先谢谢你。

2 个答案:

答案 0 :(得分:2)

您需要覆盖doPost方法,因为您正在提交表单,这是一个发布请求。

公共类AddServelet扩展了HttpServlet { 私有静态最终长serialVersionUID = 1L;

public class LoginActivity extends AppCompatActivity {

private EditText id, password;
private Button btn_login;
private TextView need_help, forgot_pass;
private ProgressBar loading;
private static String URL_LOGIN = "http://192.168.1.1/android_register_login/login.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    loading = findViewById(R.id.loading);
    id = findViewById(R.id.id);
    password = findViewById(R.id.password);
    btn_login = findViewById(R.id.btn_login);
    need_help = findViewById(R.id.need_help);
    forgot_pass = findViewById(R.id.forgot_pass);

    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String mId = id.getText().toString().trim();
            String mPass = password.getText().toString().trim();

            if (!mId.isEmpty() || !mPass.isEmpty()){
                Login(mId, mPass);
            } else {
                id.setError("Please insert your ID");
                password.setError("Please insert your password");
            }
        }
    });

private void Login(final String id, final String password) {

    loading.setVisibility(View.VISIBLE);
    btn_login.setVisibility(View.GONE);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                    JSONObject jsonObject = new JSONObject(response);
                    String success = jsonObject.getString("success");
                        JSONArray jsonArray = jsonObject.getJSONArray("login");

                        if (success.equals("1")){
                            for (int i=0; i<jsonArray.length(); i++ ){
                                JSONObject object = jsonArray.getJSONObject(i);

                                String id = object.getString("no_user").trim();
                                String level = object.getString("level").trim();

                                Toast.makeText(LoginActivity.this,
                                        "Success Login. \nYour ID : "
                                                +id+"\nYour level : "+
                                                level, Toast.LENGTH_SHORT)
                                        .show();
                                loading.setVisibility(View.GONE);

                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        loading.setVisibility(View.GONE);
                        btn_login.setVisibility(View.VISIBLE);;
                        Toast.makeText(LoginActivity.this, "Error "+e.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    loading.setVisibility(View.GONE);
                    btn_login.setVisibility(View.VISIBLE);
                    Toast.makeText(LoginActivity.this, "Error " +error.toString(), Toast.LENGTH_SHORT).show();
                }
                public void onErrorResponse(ClientError error){
                    loading.setVisibility(View.GONE);
                    btn_login.setVisibility(View.VISIBLE);
                    Toast.makeText(LoginActivity.this, "Error " +error.toString(), Toast.LENGTH_SHORT).show();
                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("id", id);
            params.put("password", password);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
}

}

答案 1 :(得分:1)

如果要处理GET请求,请覆盖HttpServlet protected void doGet(HttpServletRequest req, HttpServletResponse resp)方法