2011-08-23 55 views
11

我在嵌入碼頭,提供單個servlet和一些靜態內容。我已經從http://download.eclipse.org/jetty/7.4.5.v20110725/dist/下載了碼頭,並將所有來自JETTY_HOME/lib/*和JETTY_HOME/lib/jsp/*的jar添加到eclipe的用戶庫中;這些用戶庫已添加到我的項目中。如果我將JSP文件放在我的靜態內容文件夾(./webapps/static/)中,並在http://localhost:8080/static/test.jsp上查看它,則不會評估java表達式,並且會顯示文件的全部內容。如何使用Jetty提供JSP文件?

我錯過了什麼?

我主要的java類:

import org.eclipse.jetty.server.Handler; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.server.handler.ContextHandler; 
import org.eclipse.jetty.server.handler.HandlerList; 
import org.eclipse.jetty.server.handler.ResourceHandler; 
import org.eclipse.jetty.server.nio.SelectChannelConnector; 
import org.eclipse.jetty.servlet.ServletContextHandler; 
import org.eclipse.jetty.servlet.ServletHolder; 

public class Test { 

    public static void main(String[] args) throws Exception { 
     Server server = new Server(); 
     SelectChannelConnector connector = new SelectChannelConnector(); 
     connector.setPort(8080); 
     server.addConnector(connector);    

     // Create a resource handler for static content. 
     ResourceHandler staticResourceHandler = new ResourceHandler(); 
     staticResourceHandler.setResourceBase("./webapps/static/"); 
     staticResourceHandler.setDirectoriesListed(true);   
     //staticResourceHandler.setWelcomeFiles(new String[]{ "index.html", });   
     //staticResourceHandler.setCacheControl("no-store,no-cache,must-revalidate"); 

     // Create context handler for static resource handler. 
     ContextHandler staticContextHandler = new ContextHandler(); 
     staticContextHandler.setContextPath("/static");  
     staticContextHandler.setHandler(staticResourceHandler);   

     // Create servlet context handler for main servlet. 
     ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); 
     servletContextHandler.setContextPath("/");  
     servletContextHandler.addServlet(new ServletHolder(new HelloServlet()),"/"); 

     // Create a handler list to store our static and servlet context handlers. 
     HandlerList handlers = new HandlerList(); 
     handlers.setHandlers(new Handler[] { staticContextHandler, servletContextHandler }); 

     // Add the handlers to the server and start jetty. 
     server.setHandler(handlers); 
     server.start(); 
     server.join(); 
    }   

} 

我的JSP文件,我想爲:

<html> 
<body> 
Time: <%= new java.util.Date() %> 
</body> 
</html> 

罐子名單在JETTY_HOME/lib中/:罐子

$ ls -1 ./jetty-distribution-7.4.5.v20110725/lib/*.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-ajp-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-annotations-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-client-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-continuation-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-deploy-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-http-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-io-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-jmx-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-jndi-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-overlay-deployer-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-plus-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-policy-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-rewrite-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-security-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-server-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-servlet-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-servlets-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-util-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-webapp-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-websocket-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jetty-xml-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/servlet-api-2.5.jar 

名單在JETTY_HOME/lib/jsp /中:

$ ls -1 ./jetty-distribution-7.4.5.v20110725/lib/jsp/*.jar 
./jetty-distribution-7.4.5.v20110725/lib/jsp/com.sun.el_1.0.0.v201004190952.jar 
./jetty-distribution-7.4.5.v20110725/lib/jsp/ecj-3.6.jar 
./jetty-distribution-7.4.5.v20110725/lib/jsp/javax.el_2.1.0.v201004190952.jar 
./jetty-distribution-7.4.5.v20110725/lib/jsp/javax.servlet.jsp_2.1.0.v201004190952.jar 
./jetty-distribution-7.4.5.v20110725/lib/jsp/javax.servlet.jsp.jstl_1.2.0.v201004190952.jar 
./jetty-distribution-7.4.5.v20110725/lib/jsp/jetty-jsp-2.1-7.4.5.v20110725.jar 
./jetty-distribution-7.4.5.v20110725/lib/jsp/org.apache.jasper.glassfish_2.1.0.v201007080150.jar 
./jetty-distribution-7.4.5.v20110725/lib/jsp/org.apache.taglibs.standard.glassfish_1.2.0.v201004190952.jar 

更新:@JJ的建議幫助解決了如何使用jetty正確提供JSP文件。我現在唯一的問題是如何阻止jetty列出./webapps/jsp/的目錄內容。我目前最基本的修復方法是將index.html或index.jsp放在./webapps/jsp/中,但我更希望能夠配置jetty以返回禁止的錯誤。

import org.eclipse.jetty.server.Handler; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.server.handler.ContextHandler; 
import org.eclipse.jetty.server.handler.HandlerList; 
import org.eclipse.jetty.server.handler.ResourceHandler; 
import org.eclipse.jetty.server.nio.SelectChannelConnector; 
import org.eclipse.jetty.servlet.ServletContextHandler; 
import org.eclipse.jetty.servlet.ServletHolder; 
import org.eclipse.jetty.webapp.WebAppContext; 

public class Test { 

    public static void main(String[] args) throws Exception { 
     Server server = new Server(); 
     SelectChannelConnector connector = new SelectChannelConnector(); 
     connector.setHost("127.0.0.1"); 
     connector.setPort(8080); 
     server.addConnector(connector); 

     // Create a resource handler for static content. 
     ResourceHandler staticResourceHandler = new ResourceHandler(); 
     staticResourceHandler.setResourceBase("./webapps/static/"); 
     staticResourceHandler.setDirectoriesListed(true); 

     // Create context handler for static resource handler. 
     ContextHandler staticContextHandler = new ContextHandler(); 
     staticContextHandler.setContextPath("/static"); 
     staticContextHandler.setHandler(staticResourceHandler); 

     // Create WebAppContext for JSP files. 
     WebAppContext webAppContext = new WebAppContext(); 
     webAppContext.setContextPath("/jsp"); 
     webAppContext.setResourceBase("./webapps/jsp/"); 
     // ??? THIS DOES NOT STOP DIR LISTING OF ./webapps/jsp/ ??? 
     webAppContext.setInitParameter("dirAllowed", "false"); 

     // Create servlet context handler for main servlet. 
     ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); 
     servletContextHandler.setContextPath("/"); 
     servletContextHandler.addServlet(new ServletHolder(new HelloServlet()), "/*"); 

     // Create a handler list to store our static, jsp and servlet context handlers. 
     HandlerList handlers = new HandlerList(); 
     handlers.setHandlers(new Handler[] { staticContextHandler, webAppContext, servletContextHandler }); 

     // Add the handlers to the server and start jetty. 
     server.setHandler(handlers);  
     server.start(); 
     server.join(); 
    } 

} 

回答

6

我認爲你錯過了一些關鍵的部分,它告訴Jetty將服務文件解析爲JSP內容而不是簡單的靜態文件。我不能清楚地看到你錯過了什麼,而不能調試代碼,但是我建議你檢查有關嵌入這個參考碼頭:

http://docs.codehaus.org/display/JETTY/Embedding+Jetty

特別是我覺得這段代碼可能有關您的問題:

// assumes that this directory contains .html and .jsp files 
// This is just a directory within your source tree, and can be exported as part of your normal .jar 
final String WEBAPPDIR = "com/xxx/yyy/webapp"; 
final Server server = new Server(httpServerPort); 
final String CONTEXTPATH = "/admin"; 

// for localhost:port/admin/index.html and whatever else is in the webapp directory 
final URL warUrl = this.class.getClassLoader().getResource(WEBAPPDIR); 
final String warUrlString = warUrl.toExternalForm(); 
server.setHandler(new WebAppContext(warUrlString, CONTEXTPATH)); 
相關問題