J2EE中HTTP method GET/Post is not supported by this URL

简介:
 

原因:

1,继承自HttpServlet的Servlet没有重写对于请求和响应的处理方法:doGet或doPost等方法;默认调用父类的doGet或doPost等方法;

2,父类HttpServlet的doGet或doPost等方法覆盖了你重写的doGet或doPost等方法;

不管是1或2,父类HttpServlet的doGet或doPost等方法的默认实现是返回状态代码为405的HTTP错误表示对于指定资源的请求方法不被允许。

 

解决方法:

1,子类重写doGet或doPost等方法;

2,在你扩展的Servlert中重写doGet或doPost等方法来处理请求和响应时不要调用父类HttpServlet的doGet或doPost等方法,即去掉super.doGet(request, response)和super.doPost(request, response);

 

值得注意的是

转发到另一个action并不会改变转发方式,也就是说,我在这个action里面的doPost方法转发给另一个action,另一个action必须在它的doPost方法里面接收

package com.xy.action;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.ITopicDao;
import com.xy.dao.impl.TopicDaoImpl;
import com.xy.entity.Topic;

public class PostAction extends HttpServlet
{

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {
  ITopicDao itd = new TopicDaoImpl();

  String title = request.getParameter("title");
  String content = request.getParameter("content");
  String pt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
  String mt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
  int bid = Integer.valueOf(request.getParameter("boardId"));
  int uid = Integer.valueOf(request.getParameter("uId"));

  Topic t = new Topic();
  t.setTitle(title);
  t.setBoardId(bid);
  t.setContent(content);
  t.setPublishTime(pt);
  t.setmodifyTime(mt);
  t.setUid(uid);
  itd.addTopic(t);

  RequestDispatcher dis = request.getRequestDispatcher("ToListAction");
  dis.forward(request, response);
 }

}

也就是说:相应在ToListAction这个action里面,必须重写doPost方法。

目录
相关文章
|
3月前
|
前端开发 JavaScript Java
如何捕获和处理HTTP GET请求的异常
如何捕获和处理HTTP GET请求的异常
|
3月前
|
存储 缓存 网络协议
计算机网络常见面试题(二):浏览器中输入URL返回页面过程、HTTP协议特点,GET、POST的区别,Cookie与Session
计算机网络常见面试题(二):浏览器中输入URL返回页面过程、HTTP协议特点、状态码、报文格式,GET、POST的区别,DNS的解析过程、数字证书、Cookie与Session,对称加密和非对称加密
|
3月前
|
缓存 安全 API
http 的 get 和 post 区别 1000字
【10月更文挑战第27天】GET和POST方法各有特点,在实际应用中需要根据具体的业务需求和场景选择合适的请求方法,以确保数据的安全传输和正确处理。
|
4月前
|
弹性计算 安全 API
HTTP 405 Method Not Allowed:解析与解决
本文详细解析了HTTP 405 "Method Not Allowed" 错误,包括其定义、常见原因、示例代码及解决方案。通过检查API文档、修改请求方法或更新服务器配置,可有效解决此错误,提升Web开发效率。
1165 2
|
4月前
|
JSON 编解码 安全
【HTTP】方法(method)以及 GET 和 POST 的区别
【HTTP】方法(method)以及 GET 和 POST 的区别
161 1
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Found lingering reference异常 ERROR: Found lingering reference file hdfs://jiujiang1:9000/hbase/month_hotstatic/...
738 0
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
java链接MongoDB处理大量数据时经常碰到cursor not found 的异常,其实是超时所致 Exception in thread "main" com.
847 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
负载均衡: LVS(Layer 4), HAProxy(Layer 4、 7),Nginx(Layer 7) 虚拟化: LXC、KVM、Xen HA:Keepalived、Heartbeat 分布式缓存...
776 0
|
Web App开发 监控 前端开发