2011-08-23 19 views
11

Osadzam pomost, serwuję pojedynczy serwlet i statyczną zawartość. Pobrałem pomost z http://download.eclipse.org/jetty/7.4.5.v20110725/dist/ i dodałem wszystkie słoiki z JETTY_HOME/lib/* i JETTY_HOME/lib/jsp/* do użytkownika librarys w eclipe; te biblioteki użytkownika zostały dodane do mojego projektu. Jeśli umieściłem plik JSP w folderze Mojej statycznej zawartości (./webapps/static/) i przejdę do niego pod http://localhost:8080/static/test.jsp, wyrażenie java nie zostanie ocenione i wyświetlona zostanie pełna zawartość pliku.Jak wyświetlać pliki JSP przy użyciu Jetty?

Czego mi brakuje?

Moje główne klasy 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(); 
    }   

} 

Mój plik JSP Chcę służyć:

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

Lista słoików w 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 

Lista słoikach w 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 

Aktualizacja: Sugestia z @JJ pomogła ustalić, jak prawidłowo obsługiwać plik JSP z pomostem. Jedyny mój problem polega teraz na tym, jak powstrzymać pomost od wpisania zawartości katalogu ./webapps/jsp/. Najbardziej podstawową poprawką jaką mam na chwilę jest umieszczenie index.html lub index.jsp w ./webapps/jsp/, ale wolałbym móc skonfigurować mola do zwrócenia niedozwolonego błędu.

Odpowiedz

6

Myślę, że brakuje jakiegoś kluczowego fragmentu, który mówi Jetty, aby przeanalizował obsługiwane pliki jako zawartość JSP, zamiast zwykłego pliku statycznego. Nie widzę dokładnie, co tracisz bez możliwości debugowania kodu jednak polecam sprawdzić to wzmianka o osadzanie Jetty:

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

W szczególności myślę, że ten kawałek kodu może być istotne Twój problem:

// 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)); 
Powiązane problemy