目录
  • 一、MVC架构
    • 1、MVC是什么
    • 2、MVC三层的主要构成
    • 3、MVC框架的作用
  • 二、回顾Servlet

    一、MVC架构

    1、MVC是什么

    • MVC是模型Model、视图View和控制器Controller的简称,是一种架构规范
    • 降低了业务逻辑与视图之间的双向耦合

    2、MVC三层的主要构成

    • Model(模型):包括数据和业务,主要是Service和Dao
    • View(视图):负责模型的展示,即用户看到的界面,例如JSP
    • Controller(控制器):接收请求,委托给model进行处理,等到处理完毕之后,将数据模型返回给视图,由视图负责展示,也就是说他起到一个中间人的作用,例如Servlet

    3、MVC框架的作用

    • 将url映射到java类或java类的方法
    • 封装用户提交的数据
    • 处理请求–调用相关的业务处理–封装响应数据
    • 将响应的数据进行渲染 . jsp / html 等表示层数据

    二、回顾Servlet

    1、什么是servlet

    Servlet 是指任何实现了这个 Servlet 接口的类,它解决了当浏览器发送请求到服务器时,服务器按照请求寻找哪个Servlet类下的代码,怎么执行的问题

    了解一下重定向和转发的异同点:

    相同点:页面都会实现跳转

    不同点:转发的地址栏url不会变,重定向会变

    2、简单的servlet实例

    首先我们需要在父工程的maven依赖中导入

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
    

    接着我们建立一个Moudle:springmvc-01-servlet , 右键添加Web app的支持

    SpringMVC MVC架构与Servlet使用详解

    然后我们新建一个MyServlet,继承HttpServlet (实际上还是实现了Servlet这个接口 )

    package com.decade.servlet;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    // 实现servlet接口
    public class MyServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 获取请求参数
            String method = req.getParameter("method");
            if ("add".equals(method)) {
                req.getSession().setAttribute("msg", "执行了add方法");
            } else if ("delete".equals(method)) {
                req.getSession().setAttribute("msg", "执行了delete方法");
            }
            // 视图跳转
            req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req, resp);
    		// 重定向使用rsp.sendRedirect("/index.jsp");
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }

    然后编写test.jsp,在WEB-INF目录下新建一个jsp的文件夹,新建test.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
      ${msg}
    </body>
    </html>
    

    接着我们需要在web.xml中注册一下servlet,指定服务启动展示的首页以及session过期时间

    <?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>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.decade.servlet.MyServlet</servlet-class>
        </servlet>
        <!-- 配置指定url将请求转发到对应的servlet
          此处就是为什么后面的http://localhost:8080/servlet/hello能将请求转发到MyServlet中进行处理
         -->
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
        <!-- session过期时间,以分钟为单位 -->
        <session-config>
            <session-timeout>5</session-timeout>
        </session-config>
        <!-- 默认欢迎页 -->
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    

    最后我们配置一下tomcat,进行测试

    SpringMVC MVC架构与Servlet使用详解

    SpringMVC MVC架构与Servlet使用详解

    最后我们使用http://localhost:8080/servlet/hello?method=add这个链接进行测试

    结果如下,符合我们的预期

    SpringMVC MVC架构与Servlet使用详解

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。