欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Javaweb Servlet获取请求类型

程序员文章站 2024-02-04 20:10:16
...
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
	System.out.println("this is hello servlet");
	//HttpServletRequest是 ServletRequest的子类
	HttpServletRequest httpServletRequest = (HttpServletRequest) req;
	// getMethod()方法可以获取当前的请求类型
	String method = httpServletRequest.getMethod();
	System.out.println("请求类型:  "+ method);
}
  • HttpServletRequest是ServletRequest 的子类,子类有getMethod()方法可以获取当前的请求类型