添加:Servlet
发布日期:2021-06-30 14:59:33 浏览次数:2 分类:技术文章

本文共 3280 字,大约阅读时间需要 10 分钟。

添加:Servlet

Servlet 2.5 配置 web.xml 添加 Servlet

  • 创建 Servlet
    /demo/src/main/java/com/jerry/demo/servlet/MyServlet.java
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8"); response.getWriter().append("Servlet 2.5 通过配置 web.xml 注册 Servlet "); }}
  • 配置 web.xml
    /demo/src/main/webapp/WEB-INF/web.xml
myServlet
com.jerry.demo.servlet.MyServlet
myServlet
/test

Servlet 3.0 添加注解 @WebServlet 添加 Servlet

Servlet 3.0 新增了注解 用来注册Servlet

直接在 MyServlet 上添加注解 @WebServlet("/test")

@WebServlet("/test")public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8"); response.getWriter().append("Servlet 3.0 使用注解 @WebServlet 注册 Servlet "); }}

Servlet 3.1 编程式 添加 Servlet

  • Servlet 3.1 支持 编程式添加和配置Servlet。web.xml不再是必须的了。

SpringBoot 零配置的源头就在于此,SpringBootSpringMVCdispatcherServlet就是从这里注册的

  • 创建 Servlet
    /demo/src/main/java/com/jerry/demo/MyServlet.java
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8"); response.getWriter().append("Servlet 3.1 编程式添加 servlet 成功!"); }}

添加个启动监听器,用来添加 servlet(这里使用监听器只是图方便,扩展知识请看 )

package com.jerry.demo.listener;@WebListenerpublic class AppContextListener implements ServletContextListener {
/** * context 初始化 */ @Override public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext(); /* 这句等同于
myServlet
com.jerry.demo.servlet.MyServlet
*/ ServletRegistration.Dynamic registration = servletContext.addServlet("myServlet", MyServlet.class ); /* 这句等同于
myServlet
/test
*/ registration.addMapping("/test"); } /** * context 销毁 */ @Override public void contextDestroyed(ServletContextEvent sce) {
System.out.println("AppContext 死翘了~~~!"); }}

URL- Patterns

watermelon
/fruit/summer/*
garden
/seeds/*
list
/seedlist
kiwi
*.abc
URL URL- Patterns Servlet Invoked
http://host:port/mywebapp/fruit/summer/index.html /fruit/summer/* watermelon
http://host:port/mywebapp/fruit/summer/index.abc /fruit/summer/* watermelon
http://host:port/mywebapp/seedlist /seedlist list
http://host:port/mywebapp/seedlist/index.html The default servlet, if configured, or an HTTP 404 File Not Found error message.If the mapping for the list servlet had been /seedlist*, the list servlet would be invoked.
http://host:port/mywebapp/seedlist/pear.abc *.abc kiwi
提醒:/seedlist* 优先级高于 kiwi
http://host:port/mywebapp/seeds /seeds/* garden
http://host:port/mywebapp/seeds/index.html /seeds/* garden
http://host:port/mywebapp/index.abc *.abc kiwi

参考资料

转载地址:https://jerryjin.blog.csdn.net/article/details/92705031 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SpringBoot 借助 Servlet 3.1 新特性实现零配置的原理
下一篇:添加:监听器(Listener)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月14日 02时45分18秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章