Hi, On Mon, Oct 10, 2022 at 12:02 PM Dung Nguyen <[email protected]> wrote: > > Hi! > > I use jetty for my server. I just wonder if there is a way to manage (get > count , list connection, force disconnect) connection ? > > For example : i want verify client before it connect and then disconnect a > client if the authentication failed, how to do that ? > > my code like this : > > server = new Server(); > ServerConnector httpConnector = new ServerConnector(server); > httpConnector.setPort(utils.server_listen_port); > httpConnector.setIdleTimeout(5000); // > httpConnector.setHost(listen_interface); // <--------- ! > server.addConnector(httpConnector); > ServletContextHandler handler = new ServletContextHandler(server, "/"); > > hello h0 = new hello(); h0.bind_to_server(handler, "/"); > > > > public class hello extends HttpServlet{ > > @Override > protected void doGet(HttpServletRequest req, HttpServletResponse resp) > throws ServletException, IOException { > try { > if (!req.getParameter("key").equals("123"){ > > disconnect the client! > > } > ... do other thing > } catch (Exception e) { > e.printStackTrace(); > write_response....(resp, "ERROR Exception " + e.getMessage()); > } > } > }
The Servlet APIs do not expose any API to close the connection. However, you can add the `Connection: close` header to the response. Adding the `Connection: close` header would only work for HTTP/1.1 (and just for that one connection, not for all the connections established by the client to the server). For other protocols such as HTTP/2 or HTTP/3, adding the `Connection: close` header will have no effect. However, HTTP Authentication is a solved problem, you should research the topic and use existing solutions, rather than trying to close the connection. In particular for Servlets, you can use authentication Filters, etc. -- Simone Bordet ---- http://cometd.org http://webtide.com Developer advice, training, services and support from the Jetty & CometD experts. _______________________________________________ jetty-users mailing list [email protected] To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
