解决Java web 中 Request method ‘GET’ not supported问题

  • Post author:
  • Post category:java


一般这种错误是发送id的时候,servlet接收不到。


<a href=”./deleteServlet?id=${emp.id}” style=”color:#000;”>删除</a>

import java.io.IOException;

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 edu.jmi.emp.dao.EmpDao;

/**

* Servlet implementation class deleteServlet

*/

@WebServlet(“/deleteServlet”)

public class deleteServlet extends HttpServlet {


private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public deleteServlet() {


super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


// TODO Auto-generated method stub

response.getWriter().append(“Served at: “).append(request.getContextPath());

doPost(request, response);

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


// TODO Auto-generated method stub

request.setCharacterEncoding(“utf-8”);

int id=Integer.parseInt(request.getParameter(“id”));

EmpDao empDao=new EmpDao();

boolean b=empDao.deleteEmp(id);

System.out.println(id);

if(b==true) {


request.getRequestDispatcher(“MainServlet”).forward(request, response);

}else {


response.sendRedirect(“index.jsp”);

}

}

}



版权声明:本文为liqz666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。