1、@WebServlet的作用
简化了配置,不用在web.xml中配置好多的 <servlet>的标签了,需要tomcat版本要7 及以上
2、用法
@WebServlet("/hello") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getParameter("method"); if(method.equals("add")){ req.getSession().setAttribute("msg","执行了add 方法"); } if(method.equals("delete")){ req.getSession().setAttribute("msg","执行了delete方法"); } //转发 req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp); //重定向 // resp.sendRedirect(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
3、测试