获取HTTP状态405 - HTTP方法此URL不支持GET

时间:2014-10-13 16:33:33

标签: http servlets

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfig1 extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException ,IOException{
            PrintWriter pw = res.getWriter();
            ServletConfig cfg = getServletConfig();
            String myname = cfg.getInitParameter("myname");
            pw.print("my name is"+myname);
            System.out.println("hello");
    }
}

的web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>ServletConfig</display-name>
    <servlet>
          <servlet-name> ServletConfig1</servlet-name>
          <servlet-class>ServletConfig1</servlet-class>
          <init-param>
          <param-name>myname</param-name>
          <param-value>saurabh</param-value>
          </init-param>
          </servlet>
          <servlet-mapping>
          <servlet-name>ServletConfig1</servlet-name>
          <url-pattern>/index</url-pattern>
          </servlet-mapping>
        </web-app>

在运行这个servletconfig示例时,即使我在我的应用程序中使用了post方法,也不支持http get方法。这可能是什么原因?

1 个答案:

答案 0 :(得分:0)

将public void doPost(HttpServletRequest req,HttpServletResponse res)更改为public void doGet(HttpServletRequest req,HttpServletResponse res)。