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方法。
最後更新:2017-04-02 22:16:08