【发布时间】:2018-03-15 13:16:27
【问题描述】:
我想为我的测试用例嵌入 Jetty 9 (v9.2.12.v20150709)。 但我无法以编程方式更改 HTTP-Session-Timeout。
这个电话webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(timeoutInSeconds); 似乎不起作用。
这是简化的代码段,显示了我的工作:
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();
}
}
我正在使用WebAppContext-风格的设置,因为这使我可以使用WebAppContext.addEventListener() 让我的ServletContextListeners 工作。我无法使用ServletHandler 来工作。
我也在使用 Jetty 的 9.2.12.v20150709 版本,因为它与 Selenium v2.5.2(支持 Java 7(项目要求))的 Classpath 兼容。
你有什么建议,我做错了什么?
感谢您的宝贵时间。
【问题讨论】:
-
您使用的是什么会话管理器?从您的代码示例中,您要么没有,要么没有虚拟的无操作版本。 (只有一些会话管理器实现支持该配置)
-
ServletHandler不能直接使用(它是一个没有 ServletContext 概念的内部类,而 ServletContextListener 需要),您应该使用ServletContextHandler或WebAppContext. -
是的,我没有明确设置会话管理器。根据source code,默认使用
HashSessionManger。反过来implementssetMaxInactiveInterval-方法。 -
ServletHandler-Variant 显示在 embedded example of Jetty 9.3 中。但正如你猜到的,我也尝试了ServletContextHandler,但我无法让 EventListeners 工作。
标签: java jetty embedded-jetty jetty-9