当编写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"); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <?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的形式)
成长就是将哭声调成静音的过程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理