当编写Servlet时出现type Status report message HTTP method GET is not supported by this URL description The specified HTTP method is not allowed for the requested resource.

  直接看我代码再看解释就懂了

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jboss.weld.context.ApplicationContext;

public class ServletLogin extends HttpServlet{
 
     private String username;
     private String password;
     
    /* public void doGet(HttpServletRequest request,HttpServletResponse reponse){
         this.username=(String)request.getParameter("username");
         this.password=(String)request.getParameter("password");
         System.out.println("123");
     }
     
     public void doPost(HttpServletRequest request,HttpServletResponse reponse){
         this.username=(String)request.getParameter("username");
         this.password=(String)request.getParameter("password");
         System.out.println("456");
     }*/
     
     
      public void init(){
          //四个域对象(servletcontext ,Session,Request,page)
          ServletConfig application=this.getServletConfig();//此对象获得局部变量
          ServletContext application1=this.getServletContext();//此对象获得全局变量
          String username=application.getInitParameter("username");
          String password=application.getInitParameter("password");
          String var=application1.getInitParameter("var");
          String var1=application.getInitParameter("var");
          System.out.println(var);
          System.out.println(var1);
          System.out.println(password);
          System.out.println(username);
          System.out.println("789");
        // if(this.username.equals(username)&&this.password.equals(password))
            //request.sendRedirect("MyJsp.jsp");
      }
     
     }
       
    
<?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>Xuguang5</display-name>
     <!-- 声明全局变量 -->
   <context-param>
	        <param-name>var</param-name>
	        <param-value>我是全局变量</param-value>   
  </context-param>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>
	    <servlet-name>ServletLogin</servlet-name>
	    <servlet-class>ServletLogin</servlet-class>
	       <!-- 声明局部变量 -->
	    <init-param>
	        <param-name>username</param-name>
	        <param-value>我是局部变量1</param-value>
	    </init-param>
	    
	    <init-param>
	        <param-name>password</param-name>
	        <param-value>我是局部变量2</param-value> 
	    </init-param>
  </servlet>
   <servlet-mapping>
        <servlet-name>ServletLogin</servlet-name>
        <url-pattern>/ServletLogin</url-pattern>
   </servlet-mapping>
   
  <!--  
   <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>LoginFilter</filter-class>
        <init-param>
        <param-name>name</param-name>
        <param-value>true1</param-value>
        </init-param>
  </filter>
  
  <filter-mapping>
	  <filter-name>LoginFilter</filter-name>
	  <url-pattern>/*</url-pattern>
	  <dispatcher>REQUEST</dispatcher>
  </filter-mapping> -->
</web-app>

  代码当我把doGet()方法注释掉后就会出现这种情况,原因是doGet()/doPost()方法主要用于处理表单提交过来的数据,在表单<form></form>中method属性由于默认的是get方式提交也就是调用doGet()方法,当是method是POST方式提交时就是调用doPost()方法。即使我们没有编写jsp,我们直接用URL访问servlet时发送的时Http请求,servlet默认访问doGet方法,但是我们程序中没有重写父类的doGet()方法,而父类的doGet()方法又不支持这种URL方式,所以出现HTTP method GET is not supported by this URL这种错误。只需要重写doGet方法就可以。

        当我们用JSP编写表单提交数据到servlet时如果我们<form>中的属性method是设置了doPOST/doGet方法,但是我们servlet还是没有重写doPOST/doGet方法还是会出现一样的问题。如下图(这里就举没有重写doPOST的形式)

 

posted @ 2017-01-18 23:23  p微笑玲jx绝影  阅读(20368)  评论(0编辑  收藏  举报