SpringMVC-Servlet

2023-12-13 06:24:08

依赖

<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 {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("msg","登录成功!");
        // 转发到目标视图
        req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

文章来源:https://blog.csdn.net/h_e_l_l_o_______/article/details/134902503
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。