注: 该章节主要为原创内容,为后续的Spring MVC内容做一个先行铺垫
1.Servlet的构建使用
(1) 选择Maven -> webapp来构建一个web应用
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509222021414-1725326899.png[/img](2) 构建好后,打开pom.xml文件,一要注意打包方式为war包,二导入servlet依赖,如下- <packaging>war</packaging>
- <dependencies>
- <?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_3_1.xsd"
- version="3.1">
-
- </web-app><dependency>
- <?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app><groupId>javax.servlet</groupId>
- <?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app><artifactId>javax.servlet-api</artifactId>
- <?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app><version>3.1.0</version>
- <?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app><scope>provided</scope>
- <?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_3_1.xsd"
- version="3.1">
-
- </web-app></dependency>
- </dependencies>
复制代码 (3) 替换webapp/WEB-INF/web.xml文件为如下内容,采用Servlet 3.1版本- <?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_3_1.xsd"
- version="3.1">
-
- </web-app>
复制代码 (4) 在main目录下,新建java目录和resources目录,并在java目录下新建包,最终项目目录结构如下
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509223138766-401565015.png[/img](5) 编写一个简单的servlet如下- @WebServlet("/example")public class ExampleServlet extends HttpServlet {<?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app>@Override<?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app>protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app>resp.getWriter().write("example...");<?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app>}}
复制代码 (6) 有了servlet后,我们得让服务器知道哪个请求要交给哪个servlet处理,因此还需要配置web.xml如下(7) 然后,为了能够在网页上访问,我们得把这个项目部署到tomcat服务器中
首先,在URL栏中,添加上项目名称,此处为springmvc
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509224914750-1271790275.png[/img]然后,在Deployment中添加我们的项目
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509225123489-498372496.png[/img]最后,注意 Application Context 中的值,应与前面在URL栏中添加的项目名称相同,此处均为springmvc
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509225327945-37398283.png[/img](8) 最后,启动tomcat服务器,在浏览器上输入 http://localhost:8080/springmvc/example ,如果能看到 example... 字符串,则说明项目配置成功
2.基于web.xml,整合Spring与Servlet
(1) 现在,web应用已经搭建好了,但是我们希望能够在该应用中使用Spring容器,该怎么办呢? 在之前的非web环境中,我们都是在main方法中创建ioc容器(如 new ClassPathXmlApplicationContext()),然后直接使用的,但是现在没有了main方法,该由谁来创建ioc容器呢? 答案就是由我们的web容器,可以在web应用初始化的时候来帮助我们创建,但创建好之后,我们该怎么获取到ioc容器呢? Servlet规定了4大作用域,分别为page域(PageContext),当前页面有效; request域(HttpServletContext),一次请求内有效; session域(HttpSession),一次会话内有效; application域(ServletContext),在当前整个web应用内有效,因此我们可以将创建好的ioc容器直接放到application域中,这样在任何位置,我们都能拿到ioc容器进行使用,具体示例如下
首先导入相关的Spring依赖包接着,修改我们的代码,配置一个普通的bean- //创建一个普通的java类public class ExampleService {<?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app>public String get() {<?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app>return "user";<?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app>}}//然后,在resources目录下新建一个springmvc.xml,并将上面的ExampleService注册为一个bean<?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_3_1.xsd"
- version="3.1">
-
- </web-app><?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_3_1.xsd"
- version="3.1">
-
- </web-app>
复制代码 接下来,我们就得让web容器来为我们创建ioc容器了,具体由谁来创建呢? Servlet有三大核心组件,即Servlet,用于处理请求;Filter,过滤器,用来拦截或修改请求;Listener,监听器,用于监听某个事件。显然,这里使用Listener最合适,那就由Listener来为我们创建ioc容器现在ioc容器有了,而且被Spring以WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE为key放到了application域中,现在我们可以在任何地方被获取到它,如下所示当然,向上面这样每次都通过get方法获取,很麻烦,我们可以借助Spring提供的工具类,在Servlet初始化的时候对Servlet进行依赖注入,如下3.基于Servlet扩展接口,整合Spring与Servlet
(1) 在上一节中,我们将ioc的创建配置于web.xml中,但此外我们还可以利用java代码的方式来创建ioc容器,可通过Servlet 3.0提供的ServletContainerInitializer接口,来在web容器启动的时候为第三方组件提供初始化的机会(例如注册Servlet等),如果要使用ServletContainerInitializer接口,那么就必须要在项目或所其依赖的jar包中的/META-INF/services目录下创建一个名称为javax.servlet.ServletContainerInitializer 的文件,而这个文件的具体内容,就是ServletContainerInitializer实现类的全限定类名称,然后,借助java的SPI技术,web容器便会加载这些实现类,通常情况下,ServletContainerInitializer这个接口通常会配合@HandlesTypes注解一起使用,而这个@HandlesTypes注解的作用就是让web容器收集我们项目中所有所指定的类,然后将这些类作为ServletContainerInitializer的onStartup方法参数传入,这样,在web容器启动的时候,我们就可以拿到这些我们所需的类然后创建它们
当然,同上面web.xml中的ContextLoaderListener,Spring也提供了一个ServletContainerInitializer接口的实现类SpringServletContainerInitializer,来创建帮助我们简化ioc容器的创建,首先在spring-mvc jar包中,就定义了一个/META-INF/services/javax.servlet.ServletContainerInitializer文件,然后,在启动时,web容器便会加载这个文件,读取里面的内容,为SpringServletContainerInitializer这个类
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230510213533730-1995015549.jpg[/img]由于在SpringServletContainerInitializer上有注解@HandlesTypes标注,而这个注解的值为WebApplicationInitializer,因此,在创建SpringServletContainerInitializer对象前,web容器会收集应用内所有WebApplicationInitializer接口的实现类,并将它们作为参数传递给onStartup方法中的webAppInitializerClasses,这样,在web容器启动时,我们就能初始化我们所指定的对象
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230510213749691-2046699001.jpg[/img]总而言之,在应用启动时,web容器会调用ServletContainerInitializer实现类(这里为SpringServletContainerInitializer)中的onStartup方法,而这个onStartup方法中又调用了@HandlesTypes注解所指定的类或接口(此处为WebApplicationInitializer)的实现类中的onStartup方法,因此,我们可以编写一个WebApplicationInitializer的实现类,来创建ioc容器,不过,Spring已经为我们提供了一个实现了WebApplicationInitializer接口的抽象类AbstractContextLoaderInitializer,它里面已经封装好了大部分的逻辑(比如将ioc容器置于application域中等),而我们所需要做的仅仅就是创建一下ioc容器而已,如下此外,不要忘了注释掉web.xml中关于Spring的相关内容,否则会产生产生两个ioc容器最后,重启项目,输入http://localhost:8080/springmvc/example,看见user字符串则说明成功
4.Spring MVC
现在,我们将Servlet与Spring ioc容器整合到了一起,但如果我们需要处理新的请求的话,我们还得继承HttpServlet来编写新的Servlet,并将其配置到web.xml中,非常麻烦,因此,Spring变为我们提供了一个全新的框架 - Spring MVC来帮助我们进行开发
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |