To use this servlet, you will be required to register your websockets with the WebSocketMappings so that it can create your websockets under the
appropriate conditions.
The most basic implementation would be as follows:
package my.example;
import JettyWebSocketServlet;
import JettyWebSocketServletFactory;
public class MyEchoServlet extends JettyWebSocketServlet
{
@Override
public void configure(JettyWebSocketServletFactory factory)
{
factory.setDefaultMaxFrameSize(4096);
factory.addMapping(factory.parsePathSpec("/"), (req,res)->new EchoSocket());
}
}
Only request that conforms to a "WebSocket: Upgrade" handshake request will trigger the WebSocketMappings handling of creating
WebSockets. All other requests are treated as normal servlet requests. The configuration defined by this servlet init parameters will
be used as the customizer for any mappings created by JettyWebSocketServletFactory#addMapping(String, JettyWebSocketCreator) during
configure(JettyWebSocketServletFactory) calls. The request upgrade may be peformed by this servlet, or is may be performed by a
WebSocketUpgradeFilter instance that will share the same WebSocketMappings instance. If the filter is used, then the
filter configuraton is used as the default configuration prior to this servlets configuration being applied.
Configuration / Init-Parameters:
| Modifier and Type | Class and Description |
|---|---|
| private class | |
| private static record |
| Modifier and Type | Field and Description |
|---|---|
| private WebSocketComponents | |
| private final JettyWebSocketServlet. | |
| private static final Logger | |
| private WebSocketMappings |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected abstract void | configure(JettyWebSocketServletFactory
the JettyWebSocketServletFactory factory)Configure the JettyWebSocketServletFactory for this servlet instance by setting default
configuration (which may be overriden by annotations) and mapping |
| public void | destroy()
Overrides jakarta. Implements jakarta. Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. |
| private FrameHandlerFactory | Returns: the instance ofFrameHandlerFactory to be used to create the FrameHandlerGet the instance of |
| public void | init()
Overrides jakarta. A convenience method which can be overridden so that there's no need to call |
| protected void | service(HttpServletRequest
the req, HttpServletResponse HttpServletRequest object that contains the request the client made of the servletthe resp)HttpServletResponse object that contains the response the servlet returns to the clientOverrides jakarta. Receives standard HTTP requests from the public |
| components | back to summary |
|---|---|
| private WebSocketComponents components | |
| customizer | back to summary |
|---|---|
| private final JettyWebSocketServlet. | |
| LOG | back to summary |
|---|---|
| private static final Logger LOG | |
| mappings | back to summary |
|---|---|
| private WebSocketMappings mappings | |
| JettyWebSocketServlet | back to summary |
|---|---|
| public JettyWebSocketServlet() | |
| configure | back to summary |
|---|---|
| protected abstract void configure(JettyWebSocketServletFactory factory) Configure the JettyWebSocketServletFactory for this servlet instance by setting default
configuration (which may be overriden by annotations) and mapping
| |
| destroy | back to summary |
|---|---|
| public void destroy() Overrides jakarta. Implements jakarta. Doc from jakarta. Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. See
| |
| getFactory | back to summary |
|---|---|
| private FrameHandlerFactory getFactory() Get the instance of
| |
| init | back to summary |
|---|---|
| public void init() throws ServletException Overrides jakarta. Doc from jakarta. A convenience method which can be overridden so that there's no need to call
Instead of overriding
| |
| service | back to summary |
|---|---|
| protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException Overrides jakarta. Doc from jakarta. Receives standard HTTP requests from the public
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | addMapping(String
the pathspec to respond on pathSpec, JettyWebSocketCreator the WebSocketCreator to use creator)Implements org. Add a WebSocket mapping to a provided |
| public Set | getAvailableExtensionNames()
Implements org. Get the names of all available WebSocket Extensions. |
| public JettyWebSocketCreator | getMapping(String
the pathspec to respond on pathSpec)Implements org. Returns the creator for the given path spec. |
| public void | register(Class<?>
the WebSocket class to use endpointClass)Implements org. Add a WebSocket mapping at PathSpec "/" for a creator which creates the endpointClass |
| public boolean | removeMapping(String
the pathspec to respond on pathSpec)Implements org. Removes the mapping based on the given path spec. |
| public void | setCreator(JettyWebSocketCreator
the WebSocketCreator to use creator)Implements org. Add a WebSocket mapping at PathSpec "/" for a creator |
| CustomizedWebSocketServletFactory | back to summary |
|---|---|
| private CustomizedWebSocketServletFactory() | |
| addMapping | back to summary |
|---|---|
| public void addMapping(String pathSpec, JettyWebSocketCreator creator) Implements org. Doc from org. Add a WebSocket mapping to a provided If mapping is added before this configuration is started, then it is persisted through stop/start of this configuration's lifecycle. Otherwise it will be removed when this configuration is stopped.
| |
| getAvailableExtensionNames | back to summary |
|---|---|
| public Set Implements org. Doc from org. Get the names of all available WebSocket Extensions. | |
| getMapping | back to summary |
|---|---|
| public JettyWebSocketCreator getMapping(String pathSpec) Implements org. Doc from org. Returns the creator for the given path spec.
| |
| register | back to summary |
|---|---|
| public void register(Class<?> endpointClass) Implements org. Doc from org. Add a WebSocket mapping at PathSpec "/" for a creator which creates the endpointClass | |
| removeMapping | back to summary |
|---|---|
| public boolean removeMapping(String pathSpec) Implements org. Doc from org. Removes the mapping based on the given path spec. | |
| setCreator | back to summary |
|---|---|
| public void setCreator(JettyWebSocketCreator creator) Implements org. Doc from org. Add a WebSocket mapping at PathSpec "/" for a creator
| |
| Modifier and Type | Field and Description |
|---|---|
| private final JettyWebSocketCreator |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public Object | createWebSocket(ServerUpgradeRequest
the request details upgradeRequest, ServerUpgradeResponse the response details upgradeResponse, Callback the callback, should only be completed by the creator if a null WebSocket object is returned. callback)Implements org. Create a websocket from the incoming request. |
| public JettyWebSocketCreator | |
| public final boolean | equals(Object
the reference object with which to compare. o)Implements abstract java. Indicates whether some other object is "equal to" this one. |
| private JettyWebSocketCreator | |
| public final int | |
| public final String | toString()
Implements abstract java. Returns a string representation of the record. |
| creator | back to summary |
|---|---|
| private final JettyWebSocketCreator creator Record Component accessed by creator(). | |
| WrappedJettyCreator | back to summary |
|---|---|
| private WrappedJettyCreator(JettyWebSocketCreator creator) | |
| createWebSocket | back to summary |
|---|---|
| public Object createWebSocket(ServerUpgradeRequest upgradeRequest, ServerUpgradeResponse upgradeResponse, Callback callback) Implements org. Doc from org. Create a websocket from the incoming request. If the creator returns null it is responsible for completing the
| |
| creator | back to summary |
|---|---|
| public JettyWebSocketCreator creator() Record Component getter of creator. | |
| equals | back to summary |
|---|---|
| public final boolean equals(Object o) Implements abstract java. Doc from java. Indicates whether some other object is "equal to" this one. In addition
to the general contract of
R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
| |
| getJettyWebSocketCreator | back to summary |
|---|---|
| private JettyWebSocketCreator getJettyWebSocketCreator() | |
| hashCode | back to summary |
|---|---|
| public final int hashCode() Implements abstract java. Doc from java. Returns a hash code value for the record.
Obeys the general contract of
| |
| toString | back to summary |
|---|---|
| public final String toString() Implements abstract java. Doc from java. Returns a string representation of the record.
In accordance with the general contract of In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.
| |