原因:
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方法。