0

我有通常的设置:一个带有登录屏幕的 web 应用程序和一个小型 Java 类,用于设置 Jetty 以启动该应用程序。

在开发过程中,每次更改强制重启的代码后,我们都会浪费几秒钟的时间来登录。(不,JRebel没有帮助,因为它不会再次运行构造函数,因此它可能会错过一些更改)。

所以我想知道是否可以通过以下方式修补 Jetty 设置:

如果我请求/index.jsp,而不是转到真正的 JSP,它应该加载一个填写典型开发用户的用户名和密码的 servlet,让他登录,然后重定向到应用程序的主 JSP。

为了确保一切安全,我将把这个自动登录代码放到测试路径中,以免意外部署。

现在的问题是:如何从 Java 代码在 Jetty 中配置 URL 重定向/重写?出于显而易见的原因,我不想触摸web.xml.

4

1 回答 1

0

按照 中的示例,我想出了以下代码:

private WebAppContext webapp;

private void configureAutoLogin() {
    ServletHolder holder = webapp.getServletHandler().newServletHolder();
    holder.setName("autologin");
    holder.setClassName( AutoLoginServlet.class.getName() );

    webapp.getServletHandler().addServlet(holder);

    ServletMapping mapping = new ServletMapping();
    mapping.setServletName(holder.getName());
    String[] paths = { "/autologin" };
    mapping.setPathSpecs( paths );

    webapp.getServletHandler().addServletMapping(mapping);
}

为了让用户更简单,我还创建了一个目录jetty/,其中包含test.html开发人员可以在其中添加指向此类 URL 的链接。为了确保这个测试 HTML 页面不会被意外部署,我将jetty/目录添加到以下的基本资源中WebAppContext

    File webAppDir = new File( "src/main/webapp" );
    Resource webAppResource = new FileResource( webAppDir.toURI().toURL() );
    Resource jettyDir = new FileResource( new File( "jetty" ).toURI().toURL() );

    ResourceCollection resources = new ResourceCollection( webAppResource, jettyDir );
    webapp.setBaseResource( resources );
于 2011-04-05T08:56:07.527 回答