2015-01-22 3 views
0

내 웹 응용 프로그램에 대한 핸들러 설정에 문제가 있습니다. 원하는 것은 doGet 및 doPost 메소드로 HTTPServlet에서 처리하는 요청을 처리하는 것입니다. (이 메소드 내에서 JSP 페이지를로드하는 방법은 무엇입니까?) 또한 정적 콘텐츠 (HTML, JS, CSS)를로드 할 수 있습니다.부두 처리기를 구성하는 방법은 무엇입니까?

나는 지금 설정하고있는 방식으로 하나 또는 다른 설정 만 할 수 있습니다. 둘 다 작동하지 않습니다.

내가 설명 할 것입니다 :

Server server = new Server(5000); 

    // This is the resource handler for JS & CSS 

    ResourceHandler resourceHandler = new ResourceHandler(); 

    resourceHandler.setResourceBase("."); 

    resourceHandler.setDirectoriesListed(false); 

    // This is the context handler for the HTTPServlet 

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 

    context.setContextPath("/"); 

    context.addServlet(new ServletHolder(new Main()),"/*"); 

    // this is the Handler list for both handlers 

    HandlerList handlerList = new HandlerList(); 

    handlerList.setHandlers(new Handler[] { context ,resourceHandler}); 

/* 

    If I add them in this order, all requests will be handled by the "context" and no static resource is loaded 

    If I invert the order, the index page page is loaded by the resource handler, which means, If I activate directory listings, it gives me a list of all directories, otherwise it's just a blank page 

    I tried working with a WebAppContext to load JSP pages but I still had some problems with which handler should handle which requests 

*/ 

    server.setHandler(handlerList); 

    server.start(); 

    server.join(); 

감사합니다

는 는

** 편집 : ** 내가 가진 문제는 내 HTTP 서블릿은 다음과 같은 방식으로 동작한다는 것입니다

는 : 는 모든 요청을 처리합니다 리소스 핸들러에는 아무 것도 남기지 않으므로 스크립트가 .js 스크립트를 요청하면 대신 빈 html 페이지를 반환합니다. 메인 서블릿없이 루트 핸들러를 사용하는 경우, 그것은 모든 정적 컨텐츠 및 JSP 페이지를로드하지 않지만, 주요 서블릿을 추가 할 때, 그것은 더 이상 정적 콘텐츠를로드이 예에서

WebAppContext root = new WebAppContext(); 

    root.setParentLoaderPriority(true); 
    root.setContextPath("/"); 
    root.setResourceBase("."); 
    root.setWelcomeFiles(new String[] {"test.jsp"}); 
    root.addServlet(new ServletHolder(new Main()),"/*"); 

    HandlerList handlerList = new HandlerList(); 
    handlerList.setHandlers(new Handler[] { root }); 

: 여기 은 예입니다 빈 html 페이지로 정적 컨텐츠에 대한 모든 요청에 ​​응답합니다.

답변

4

서블릿을 사용하여 작업 할 때 항상 서블릿 체인의 끝에 종료가 있습니다.

이 될 중 하나를 것입니다 :

  • Default404Servlet (ServletContextHandler 같은 간단한 설정 작업)
  • (전체 WebAppContext로 작업 할 때)

    • DefaultServlet 모든 당신이 원하는 경우 ResourceHandler은 정적 콘텐츠를 제공하고 있습니다. DefaultServlet을 자신의 용도로 사용하십시오 (더 나은 선택이며 더 많은 기능을 지원합니다. 퀘스트, 캐싱, 자동 GZIP, 메모리 매핑 된 파일 제공 등)

      예 :

      package jetty; 
      
      import org.eclipse.jetty.server.Server; 
      import org.eclipse.jetty.server.ServerConnector; 
      import org.eclipse.jetty.servlet.DefaultServlet; 
      import org.eclipse.jetty.servlet.ServletContextHandler; 
      import org.eclipse.jetty.servlet.ServletHolder; 
      
      public class ManyDefaultServlet 
      { 
          public static void main(String[] args) 
          { 
           System.setProperty("org.eclipse.jetty.servlet.LEVEL","DEBUG"); 
      
           Server server = new Server(); 
           ServerConnector connector = new ServerConnector(server); 
           connector.setPort(8080); 
           server.addConnector(connector); 
      
           // Setup the basic application "context" for this application at "/" 
           // This is also known as the handler tree (in jetty speak) 
           ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
           context.setContextPath("/"); 
           server.setHandler(context); 
      
           // The filesystem paths we will map 
           String homePath = System.getProperty("user.home"); 
           String pwdPath = System.getProperty("user.dir"); 
      
           // Fist, add special pathspec of "/home/" content mapped to the homePath 
           ServletHolder holderHome = new ServletHolder("static-home", DefaultServlet.class); 
           holderHome.setInitParameter("resourceBase",homePath); 
           holderHome.setInitParameter("dirAllowed","true"); 
           holderHome.setInitParameter("pathInfoOnly","true"); 
           context.addServlet(holderHome,"/home/*"); 
      
           // Lastly, the default servlet for root content 
           // It is important that this is last. 
           ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class); 
           holderPwd.setInitParameter("resourceBase",pwdPath); 
           holderPwd.setInitParameter("dirAllowed","true"); 
           context.addServlet(holderPwd,"/"); 
      
           try 
           { 
            server.start(); 
            server.dump(System.err); 
            server.join(); 
           } 
           catch (Throwable t) 
           { 
            t.printStackTrace(System.err); 
           } 
          } 
      } 
      
    +0

    덕분에, 정적 컨텐츠와 서블릿의 내용이 지금 동시에 작업하고 있지만, 로딩 JSP 페이지를로드하는 것은 아니다 일하는 (나는 그 빈 페이지를 얻고있다). HTTPServlet JSP 페이지가 제대로로드되지 않았다는 것을주의하십시오. –

    +0

    JSP가 지원되는 Embedded Jetty는 훨씬 더 많은 설정이 필요합니다. 공식 예제 (https://github.com/jetty-project/embedded-jetty-jsp) –

    +0

    고마워, 이미 거기있어. 나는 이렇게하고 있고 일하고있다. 연습이 얼마나 나쁜가? response.sendRedirect ("jsp/index.jsp"); –

    관련 문제