405 错误?百思不得其解,请赐教

tangwei_6 2009-12-17 11:09:00
写一个Servlet页面跳转时,出现了HTTP Status 405 HTTP method POST is not supported by this URL 这个错误。

我的servlet的跳转代码如下:

request.getRequestDispatcher("/servlet/GetBlogServlet?id="+blog_id).forward(request, response);

在Servlet中使用这种方式跳转到另一个Servlet时就会出现如题的错误,改用sendRedirect(),即可解决此问题了:

response.sendRedirect("http://localhost:81/blog/servlet/GetBlogServlet?id="+blog_id);或者response.sendRedirect("/blog/servlet/GetBlogServlet?id="+blog_id);注:blog是应用的名字(工程名为blog)

redirect和forward的区别:

1) redirect 方式
response.sendRedirect("test.jsp");
页面的路径是绝对路径。sendRedirect可以将页面跳转到任何页面,不一定局限于本web应用中,如:
response.sendRedirect("http://www.baidu.com");

跳转后浏览器地址栏变化,会变成你跳转到的页面的地址。
这种方式要传值出去的话,只能在url中带parameter或者放在session中,无法使用request.setAttribute来传递。

2) forward方式
RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");
dispatcher .forward(request, response);
页面的路径是相对路径。forward方式只能跳转到本web应用中的页面上。

跳转后浏览器地址栏不会变化。
使用这种方式跳转,传值可以使用三种方法:url中带parameter,session,request.setAttribut()

小妹百思不得其解,因为我的这两个servlet同属一个web应用啊,并不是不同的应用,request为什么不可以呢?希望高手指教!
...全文
173 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangwei_6 2009-12-23
  • 打赏
  • 举报
回复
我又仔细看了下书和程序调用关系,GetBlogServlet中既用到了doPost又用到了doGet。在而以前我只是写了doPost方法,所以才会导致上述错误。谢谢各位!
shine333 2009-12-18
  • 打赏
  • 举报
回复
首先,那个/servlet/GetBlogServlet不接受POST,只接受GET。
你request.getRequestDispatcher("/servlet/GetBlogServlet?id="+blog_id).forward(request, response);代码所在的功能本身是POST请求,forward之后,仍然是POST,
而redirect无论之前的请求是POST还是GET,redirect之后都是GET
AGump1002 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 tangwei_6 的回复:]
引用 15 楼 agump1002 的回复:
request.setAttribute("id",blog_id);
request.getRequestDispatcher("/servlet/GetBlogServlet").forword(request,response);

然后request.getAttrubute("id");取值

我在这个servlet中写法如下:
request.setAttribute("id", blog_id);
request.getRequestDispatcher("/servlet/GetBlogServlet").forward(request, response);
在GetBlogServlet的doGet()中String id = (String)request.getAttribute("id");
依然不正确。
而在本servlet中response.sendRedirect("/blog/servlet/GetBlogServlet?blog_id=" + blog_id);在GetBlogServlet的doGet()中String id = request.getParameter("id");就正确了。不知道为什么
[/Quote]


路径问题。。。


servlet/GetBlogServlet

路径问题,比较麻烦,一直不很透彻,看下你jsp页面访问之后是什么样子的,不行,改servlet/GetBlogServlet
jumpheightway 2009-12-18
  • 打赏
  • 举报
回复
发报机只能够作最终发送
tangwei_6 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 agump1002 的回复:]
request.setAttribute("id",blog_id);
request.getRequestDispatcher("/servlet/GetBlogServlet").forword(request,response);

然后request.getAttrubute("id");取值
[/Quote]
我在这个servlet中写法如下:
request.setAttribute("id", blog_id);
request.getRequestDispatcher("/servlet/GetBlogServlet").forward(request, response);
在GetBlogServlet的doGet()中String id = (String)request.getAttribute("id");
依然不正确。
而在本servlet中response.sendRedirect("/blog/servlet/GetBlogServlet?blog_id=" + blog_id);在GetBlogServlet的doGet()中String id = request.getParameter("id");就正确了。不知道为什么
c0kaishi 2009-12-18
  • 打赏
  • 举报
回复
可以在doPost方法里面掉用doGet,或者在doGet里调用doPost
AGump1002 2009-12-18
  • 打赏
  • 举报
回复
request.setAttribute("id",blog_id);
request.getRequestDispatcher("/servlet/GetBlogServlet").forword(request,response);

然后request.getAttrubute("id");取值
nangonghaopeng 2009-12-18
  • 打赏
  • 举报
回复
好好看看get和post请求的区别
tangwei_6 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 final_xt 的回复:]
HTTP Status 405  HTTP method POST is not supported by this URL

你的这种方式只适合Post传值,而你的那个Url"/servlet/GetBlogServlet?id="+blog_id是get方式的。

要传id的话,就只能--》request.setAttribute("id",new Integer(blog_id));
[/Quote]
是不是说forward()无法在后面带参数传递,比如servlet?id=blog_id,这样不行,而要用request就必须用setAttribute?而response那个就可以带参数?
我试一下啊
tangwei_6 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 final_xt 的回复:]
HTTP Status 405  HTTP method POST is not supported by this URL

你的这种方式只适合Post传值,而你的那个Url"/servlet/GetBlogServlet?id="+blog_id是get方式的。

要传id的话,就只能--》request.setAttribute("id",new Integer(blog_id));
[/Quote]
我的GetBlogServlet确实是用doGet()方法,你是说我request.getRequestDispatcher("/servlet/GetBlogServlet?id="+blog_id).forward(request, response); 只适合GetBlogServlet得用doPost()方法?
夜空守望者i 2009-12-17
  • 打赏
  • 举报
回复
request.getRequestDispatcher("/servlet/GetBlogServlet?id="+blog_id).forward(request, response);

你的?id="+blog_id 有问题的,如果你是同同一个请求的话,可以直接把id存放到request中,不需要这样传值的,而且这样传值也是有问题的,
而redirect不属于同一请求,因此它的参数是可以这样用的
tangwei_6 2009-12-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huguang 的回复:]
注意服务器端跳转,forward并不是一个客户端提交。
[/Quote]
说的有点深奥,具体解释下行么?
tangwei_6 2009-12-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 whereusejava 的回复:]
404问题考虑方向:
业务逻辑定位的找不到!
参数不对!
[/Quote]
我这个是405
tangwei_6 2009-12-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huguang 的回复:]
request.getRequestDispatcher("/servlet/GetBlogServlet?id="+blog_id).forward(request, response);
这个URL中的?id="+blog_id不能要
[/Quote]
blog_id我要传过去的,为什么有这个参数就不行呢?
huguang 2009-12-17
  • 打赏
  • 举报
回复
注意服务器端跳转,forward并不是一个客户端提交。
whereusejava 2009-12-17
  • 打赏
  • 举报
回复
404问题考虑方向:
业务逻辑定位的找不到!
参数不对!
huguang 2009-12-17
  • 打赏
  • 举报
回复
request.getRequestDispatcher("/servlet/GetBlogServlet?id="+blog_id).forward(request, response);
这个URL中的?id="+blog_id不能要
lovepay1413 2009-12-17
  • 打赏
  • 举报
回复
9楼好样的。。。。用post的话就不能同时用get。。。
AspireHouse 2009-12-17
  • 打赏
  • 举报
回复
9楼正解!
final_xt 2009-12-17
  • 打赏
  • 举报
回复
HTTP Status 405 HTTP method POST is not supported by this URL

你的这种方式只适合Post传值,而你的那个Url"/servlet/GetBlogServlet?id="+blog_id是get方式的。

要传id的话,就只能--》request.setAttribute("id",new Integer(blog_id));
加载更多回复(1)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧