我使用注释编写控制器来映射控制器。 在页面jsp我有一个ajax请求到控制器但我得到错误 “ 类型:状态报告
消息:此URL不支持HTTP方法GET
description:请求的资源不允许使用指定的HTTP方法。 “ 为什么会这样?
控制器类:
package controllers;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.AntiXss;
import model.HibernateToDoListDAO;
import model.Items;
import model.ToDoListDAOException;
import model.User;
@WebServlet("/DeleteMessage/*")
public class DeleteMessageController extends HttpServlet{
private static final long serialVersionUID = 1L;
HttpSession session = null;
User user = null;
Items item = null;
List<Items> items = null;
String message;
HibernateToDoListDAO actions = HibernateToDoListDAO.getInstance();
public DeleteMessageController()
{
super();
}
@Override
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
session = request.getSession(false);
if(session == null)
{
response.sendRedirect("error.jsp");
}
try{
message = request.getParameter("item");
if(AntiXss.isUsername(message) == false )
{
if(session != null){
session.invalidate();
}
response.sendRedirect("error.jsp");
}
else
{
items = (List<Items>)session.getAttribute("listItems");
user = (User)session.getAttribute("user");
for(Items newItem : items)
{
if(newItem.getMessage() == message && newItem.getUsername() == user.getUsername())
{
item = newItem;
}
}
actions.deleteItems(item);
items = actions.getItems(user.getUsername());
session.setAttribute("listItems", items);
request.getRequestDispatcher("Index.jsp").forward(request, response);
}
}catch(ToDoListDAOException e){
e.printStackTrace();
}
}
}
和jsp页面:(我在这里只是ajax请求);
var xmlhttp = new XMLHttpRequest();
var item = "test";
if((txtSearch(item, item.length)) == 1)
{
var url = "DeleteMessage";
try
{
xmlhttp.open("post",url,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("item=" + item);
}catch(e){
alert("unable to connect to server");
}
}