Class JettyWebSocketServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable

public abstract class JettyWebSocketServlet extends jakarta.servlet.http.HttpServlet
Abstract Servlet used to bridge the Servlet API to the WebSocket API.

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:

idleTimeout
set the time in ms that a websocket may be idle before closing
maxTextMessageSize
set the size in UTF-8 bytes that a websocket may be accept as a Text Message before closing
maxBinaryMessageSize
set the size in bytes that a websocket may be accept as a Binary Message before closing
inputBufferSize
set the size in bytes of the buffer used to read raw bytes from the network layer
*
outputBufferSize
set the size in bytes of the buffer used to write bytes to the network layer
maxFrameSize
The maximum frame size sent or received.
autoFragment
If true, frames are automatically fragmented to respect the maximum frame size.
See Also:
  • Field Summary

    Fields inherited from class jakarta.servlet.http.HttpServlet

    LEGACY_DO_HEAD
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract void
    Configure the JettyWebSocketServletFactory for this servlet instance by setting default configuration (which may be overriden by annotations) and mapping JettyWebSocketCreators.
    void
     
    void
     
    protected void
    service(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)
     

    Methods inherited from class jakarta.servlet.http.HttpServlet

    doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, init, service

    Methods inherited from class jakarta.servlet.GenericServlet

    getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, log, log

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JettyWebSocketServlet

      public JettyWebSocketServlet()
  • Method Details

    • configure

      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 JettyWebSocketCreators. This method assumes a single FrameHandlerFactory will be available as a bean on the ContextHandler, which in practise will mostly the the Jetty WebSocket API factory.
      Parameters:
      factory - the JettyWebSocketServletFactory
    • init

      public void init() throws jakarta.servlet.ServletException
      Overrides:
      init in class jakarta.servlet.GenericServlet
      Throws:
      jakarta.servlet.ServletException
    • destroy

      public void destroy()
      Specified by:
      destroy in interface jakarta.servlet.Servlet
      Overrides:
      destroy in class jakarta.servlet.GenericServlet
    • service

      protected void service(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws jakarta.servlet.ServletException, IOException
      Overrides:
      service in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
      IOException