如何使用Jetty提供JSP文件? [英] How to serve JSP files using Jetty?

查看:119
本文介绍了如何使用Jetty提供JSP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在嵌入码头,服务一个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表达式,并显示文件的全部内容.

I'm embedding jetty, serving a single servlet and some static content. I've downloaded jetty from http://download.eclipse.org/jetty/7.4.5.v20110725/dist/, and added all jars from JETTY_HOME/lib/* and JETTY_HOME/lib/jsp/* to user librarys in eclipe; these user librarys have been added to my project. If I put a JSP file in my my static content folder (./webapps/static/) and view it at http://localhost:8080/static/test.jsp, the java expression are not evaluated and I get shown the full contents of the file.

我想念什么?

我的主要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文件:

My JSP file I want to serve:

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

JETTY_HOME/lib/中的罐子列表:

List of jars in 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/中的jar列表:

List of jars in 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的建议有助于确定如何正确使用码头服务JSP文件.我现在唯一的问题是如何阻止Jetty列出./webapps/jsp/的目录内容.目前,我最基本的解决方法是将index.html或index.jsp放在./webapps/jsp/中,但我希望能够配置jetty返回一个禁止的错误.


Update: The suggestion from @JJ helped work out how to correctly serve JSP file with jetty. My only issue now is how to stop jetty from listing the directory contents of ./webapps/jsp/. The most basic fix I have for the moment is to put index.html or index.jsp in ./webapps/jsp/, but I would prefer be able to configure jetty to return a forbidden error.

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();
    }

}

推荐答案

我认为您缺少一些关键内容,这些内容告诉Jetty将提供的文件解析为JSP内容而不是简单的静态文件.在无法调试代码的情况下,我无法确切看到丢失的内容,但是我建议您检查一下有关嵌入Jetty的参考:

I think you are missing some key piece which tells Jetty to parse the served files as JSP content instead of simply a static file. I can't see exactly what you're missing without being able to debug your code however I recommend you check this reference about embedding Jetty:

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

我特别认为这段代码可能与您的问题有关:

In particular I think this piece of code may be relevant to your issue:

// 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));

这篇关于如何使用Jetty提供JSP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆