依赖
<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version>
</dependency>
web.xml
4.0版本
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet><!--起个名称--><servlet-name>login</servlet-name><!--映射一个类--><servlet-class>org.example.servlet.MyHttpServlet</servlet-class></servlet><servlet-mapping><!--访问url-pattern配置的路径会进入servlet-name映射的类中--><servlet-name>login</servlet-name><url-pattern>/login</url-pattern></servlet-mapping>
</web-app>
映射类
继承HttpServlet,重写请求方法即可
public class MyHttpServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.getSession().setAttribute("msg","登录成功!");// 转发到目标视图req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}