我想将Jetty 9 (v9.2.12.v20150709)嵌入到我的测试用例中。但我无法以编程方式更改HTTP会话超时。
这个调用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();
}
}我使用的是setup的WebAppContext-Style,因为它允许我通过使用WebAppContext.addEventListener()来让我的ServletContextListener工作。我不能通过使用ServletHandler来工作。
另外,我使用的是Jetty的版本9.2.12.v20150709,因为它与Selenium v2.5.2 (支持Java7(项目需求))类路径兼容。
你有什么建议吗,我哪里做错了?
谢谢您抽时间见我。
发布于 2018-03-16 15:45:13
WebAppContext有一些默认值,这些默认值是在server.start() (WebAppContext.startContext())期间加载的。这些缺省值还包含位于/org/eclipse/jetty/webapp/webdefault.xml下的jetty-webapp.jar中的DefaultWebDescriptor。此描述符包括一个session-config,它将超时设置为默认值30m (1800s)。
要覆盖默认值,必须在服务器启动后调用setMaxInactiveInterval():
server.start();
webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(timeoutInSeconds);或者,为了避免这些默认值,使用ServletContextHandler可能会更好。
https://stackoverflow.com/questions/49300484
复制相似问题