Jetty中的ServletHandler和ServletContextHandler有什么区别? [英] What's the difference between a ServletHandler and a ServletContextHandler in Jetty?

查看:1025
本文介绍了Jetty中的ServletHandler和ServletContextHandler有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用嵌入式Jetty服务器.我只想根据请求路径将请求映射到不同的servlet.

I'm trying to get started with an embedded Jetty server. I just want to map requests to different servlets based on the request path.

创建ServletHandler并向其中添加servlet与创建ServletContextHandler并向其中添加servlet有什么区别?

What's the difference between creating a ServletHandler and adding servlets to it as opposed to creating a ServletContextHandler and adding servlets to that?

例如:

//how is this different...
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(MyServlet.class, "/path");

//from this?
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.addServlet(MyServlet.class, "/path");

推荐答案

大多数Servlet都需要 javax.servlet.ServletContext 对象才能正常运行.

Most Servlet's require a javax.servlet.ServletContext object to operate properly.

使用ServletContextHandler将为该ServletContextHandler中的所有Servlet,过滤器,会话,安全性等创建和管理公共ServletContext.这还包括正确的初始化,加载顺序以及受ServletContext影响的组件的破坏.

Using a ServletContextHandler will create and manage the common ServletContext for all of the Servlets, Filters, Sessions, Security, etc within that ServletContextHandler. This includes proper initialization, load order, and destruction of the components affected by a ServletContext as well.

还请注意,ServletHandler被视为ServletContextHandler的内部类,并不意味着像Jetty一样原始"使用.尽管从技术上讲是可行的,但除最幼稚和最简单的Servlet实现外,不鼓励使用它.

Also note, that ServletHandler is considered an internal class of ServletContextHandler and is not meant to be used "in the raw" like that with Jetty. While it is technically possible, it is discouraged for all but the most naive and simplistic implementations of a Servlet.

这篇关于Jetty中的ServletHandler和ServletContextHandler有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆