当编写Servlet时出现type Status report message HTTP method GET is not supported by this URL description The

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");
      }
     

     }



代码当我把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的形式)

 还有一种情况也会出现相同的情况就是包的路径错误导致的。如下图(只需要把的有红叉包remove即可)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值