如何在 Eclipse IDE 中使用 jsp 将 jetty 嵌入到动态 Web 项目中
我在 Wicket 项目中使用嵌入式码头,没有任何问题,因为 wicket 不使用 JSP。直到我遇到一个使用嵌入式 Jetty 应用程序服务器创建基本动态 Web 应用程序的任务。
我知道默认情况下jetty不支持JSP。我还知道还有其他几个 Servlet 容器可供选择。但对于这项任务,我想展示使用 jetty 进行 Web 开发是多么强大和简单,因为我已经在大型项目中使用它并取得了巨大成功。
我使用 Eclipse Helios 作为 IDE,并将以下 jar 添加到我的类路径
- junit-4.8.1.jar
- jetty-6.1.25.jar
- jetty-management-6.1.25.jar
- jetty-util-6.1.25.jar
- log4j -1.2.14.jar
- servlet-api-2.5-6.1.4.jar
- jsp-2.1-6.1.5.jar
- javax.servlet.jar
这是我用来启动嵌入式jetty的代码
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;
public class Start {
public static void main(String[] args) throws Exception {
Server server = new Server();
SocketConnector connector = new SocketConnector();
// Set some timeout options to make debugging easier.
connector.setMaxIdleTime(1000 * 60 * 60);
connector.setSoLingerTime(-1);
connector.setPort(8080);
server.setConnectors(new Connector[] { connector });
WebAppContext bb = new WebAppContext();
bb.setServer(server);
bb.setContextPath("/");
bb.setWar("WebContent");
server.addHandler(bb);
try {
System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
server.start();
System.in.read();
System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
// while (System.in.available() == 0) {
// Thread.sleep(5000);
// }
server.stop();
server.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(100);
}
}
}
下面是错误消息
2011-02-11 09:18:12.953:INFO::通过 org.mortbay.log.StdErrLog 记录到 STDERR 启动嵌入式 JETTY 服务器,按任意键停止 2011-02-11 09:18:13.000:信息::jetty-6.1.25 2011-02-11 09:18:13.156:警告::失败的jsp:java.lang.NoClassDefFoundError:javax / servlet / jsp / JspApplicationContext 2011-02-11 09:18:13.171:警告::失败 org.mortbay.jetty.webapp.WebAppContext@76cbf7{/,WebContent}: java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext 2011-02-11 09:18:13.171:WARN::启动处理程序时出错 java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext 在org.apache.jasper.compiler.JspRuntimeContext。(JspRuntimeContext.java:103) 在 org.apache.jasper.servlet.JspServlet.init(JspServlet.java:134) 在 org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440) 在 org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:263) 在 org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) 在 org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685) 在 org.mortbay.jetty.servlet.Context.startContext(Context.java:140) 在 org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1272) 在 org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517) 在 org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:489) 在 org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) 在 org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130) 在 org.mortbay.jetty.Server.doStart(Server.java:224) 在 org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) 在 com.pldt.embededserver.Start.main(Start.java:36) 2011-02-11 09:18:13.203:INFO::开始 [电子邮件受保护]< /a>:8080
我希望你们能帮助我。我感到非常失望(对于jetty的相关文档),由于文档不完善,像这样一个非常简单的任务变得非常混乱和耗时,即使是所有任务中最简单的(如何使用JSP运行Web应用程序)特别是对于那些想学习java并考虑使用jetty作为Servlet容器的人来说会很难。
提前致谢。
I am using an embedded jetty for my Wicket projects with no problem since wicket does not use JSP. Until I run across a task to create a basic Dynamic Web Application using an embedded jetty application server.
I know that by default jetty does not support JSPs. I'm also aware that there are several other Servlet containers to choose from. But for this task, I'd like to show how powerful and simple web development is using jetty since I have been using it with great success for larger projects.
I'm using Eclipse Helios as my IDE and added the following jars to my classpath
- junit-4.8.1.jar
- jetty-6.1.25.jar
- jetty-management-6.1.25.jar
- jetty-util-6.1.25.jar
- log4j-1.2.14.jar
- servlet-api-2.5-6.1.4.jar
- jsp-2.1-6.1.5.jar
- javax.servlet.jar
here is the code i use to start my embeded jetty
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;
public class Start {
public static void main(String[] args) throws Exception {
Server server = new Server();
SocketConnector connector = new SocketConnector();
// Set some timeout options to make debugging easier.
connector.setMaxIdleTime(1000 * 60 * 60);
connector.setSoLingerTime(-1);
connector.setPort(8080);
server.setConnectors(new Connector[] { connector });
WebAppContext bb = new WebAppContext();
bb.setServer(server);
bb.setContextPath("/");
bb.setWar("WebContent");
server.addHandler(bb);
try {
System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
server.start();
System.in.read();
System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
// while (System.in.available() == 0) {
// Thread.sleep(5000);
// }
server.stop();
server.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(100);
}
}
}
And below is the error message
2011-02-11 09:18:12.953:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP
2011-02-11 09:18:13.000:INFO::jetty-6.1.25
2011-02-11 09:18:13.156:WARN::failed jsp: java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext
2011-02-11 09:18:13.171:WARN::failed org.mortbay.jetty.webapp.WebAppContext@76cbf7{/,WebContent}: java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext
2011-02-11 09:18:13.171:WARN::Error starting handlers
java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext
at org.apache.jasper.compiler.JspRuntimeContext.(JspRuntimeContext.java:103)
at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:134)
at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)
at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:263)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1272)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:489)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.Server.doStart(Server.java:224)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.pldt.embededserver.Start.main(Start.java:36)
2011-02-11 09:18:13.203:INFO::Started [email protected]:8080
I hope you guys can help me with this. I feel so disappointed (with jetty's documentation with regards to this) that a very simple task like this turns out to be very confusing and time consuming due to poor documentation that even the simplest of all task (how to run a web application with JSP) would be so hard specially for people who wants to study java and considering jetty as their Servlet container.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您只是缺少几个 jar 文件。
但我要说的是,我认为你对文档的抱怨是不恰当的。 Jetty 与 JSP 配合良好,独立的 Jetty 和 Jetty-Maven 插件都被设计为自动包含 JSP 支持。
您选择做的是通过嵌入 Jetty 来构建您自己的服务器,然后您未能包含使其工作所需的 jar 文件。
嵌入 Jetty 是一个高级用例,文档假设您知道自己在做什么。
现在,至于您遇到的问题...
在 Jetty 发行版内有一个
lib/
目录。在该
lib/
目录内有一个jsp-2.1
目录。您需要将该目录中的所有 jar 文件包含在类路径中。
您似乎缺少
(它们的版本可能略有不同在 Jetty 6.1.25 中 - 我从 6.1.26 中提取了这些)
It looks like you're simply missing a couple of jar file.
Le me say though, that I think your complaints about documentation are out of place. Jetty works fine with JSP, and both standalone Jetty and the Jetty-Maven-plugin are designed to automatically include JSP support.
What you've chosen to do, is build you own server by embedding Jetty, and then you've failed to include the necessary jar files to make that work.
Embedding Jetty is an advanced use-case, and the documentation assumes that you know what you're doing.
Now, as to the problem you've run into...
Inside the Jetty distribution there is a
lib/
directory.Inside that
lib/
directory there is ajsp-2.1
directory.You need to include all the jar files from that directory on your classpath.
You seem to be missing
(They might be slightly different versions in Jetty 6.1.25 - I pulled these from 6.1.26)