Java e> ServletContextHandler或WebAppContext。是的,我没有明确设置会话管理器。根据,默认情况下使用HashSessionManger。它依次是setMaxInactiveInterval-方法。ServletHandler-
Java e> ServletContextHandler或WebAppContext。是的,我没有明确设置会话管理器。根据,默认情况下使用HashSessionManger。它依次是setMaxInactiveInterval-方法。ServletHandler-,java,jetty,embedded-jetty,jetty-9,Java,Jetty,Embedded Jetty,Jetty 9,e> ServletContextHandler或WebAppContext。是的,我没有明确设置会话管理器。根据,默认情况下使用HashSessionManger。它依次是setMaxInactiveInterval-方法。ServletHandler-变量显示在。但正如您所猜测的,我也尝试了ServletContextHandler,但无法让EventListeners工作。如果使用WebAppContext,您可以提供在默认描述符上提供配置覆盖。如果使用WebAppContext,则,您可
e> ServletContextHandler或
WebAppContext
。是的,我没有明确设置会话管理器。根据,默认情况下使用HashSessionManger
。它依次是setMaxInactiveInterval
-方法。ServletHandler
-变量显示在。但正如您所猜测的,我也尝试了ServletContextHandler
,但无法让EventListeners工作。如果使用WebAppContext
,您可以提供在默认描述符上提供配置覆盖。如果使用WebAppContext
,则,您可以提供,以在默认描述符之上提供配置覆盖。
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
@SuppressWarnings("javadoc")
public class EmbeddedJetty
{
@SuppressWarnings("serial")
public static class TimeoutServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
// return the value of the currently used HTTP-Session Timeout
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Timeout: " + request.getSession()
.getMaxInactiveInterval() + "</h1>");
}
}
public static void main(String[] args) throws Exception
{
// the custom timeout, which I am trying to set
int timeoutInSeconds = 1234;
Server server = new Server(0);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setResourceBase(System.getProperty("user.dir"));
// Can't set custom timeout. Following Statement doesn't work.
webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(
timeoutInSeconds);
server.setHandler(webapp);
webapp.addServlet(TimeoutServlet.class.getName(), "/*");
server.start();
// get current URL of the server
String url = server.getURI().toString();
System.out.println("\n URL: " + url);
// server.dumpStdErr();
// make a request to get the used timeout setting
HttpClient httpClient = new HttpClient();
httpClient.start();
ContentResponse response = httpClient.GET(url);
httpClient.stop();
String timeoutInfo = response.getContentAsString();
System.out.println(timeoutInfo);
// check if the custom timeout is used
if( timeoutInfo.contains(String.valueOf(timeoutInSeconds)) )
{
System.out.println("Custom Timeout is used.");
}
else
{
// Unfortunately, I get the default(?) everytime
System.out.println("Default Timeout? Custom Value is NOT used.");
}
System.out.println("Press Enter to exit ...");
System.in.read();
server.stop();
server.join();
}
}
server.start();
webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(timeoutInSeconds);