gpt4 book ai didi

java - 爆发 war 中的嵌入式jetty服务器

转载 作者:行者123 更新时间:2023-12-01 13:22:19 24 4
gpt4 key购买 nike

在通过jetty成功运行WAR之后,我尝试将jetty服务器嵌入到我的网络应用程序中。它是一个由 servlet 组成的普通 java 应用程序。我使用了分解的 WAR 目录(WARless)。

但我遇到以下异常。

    2014-02-22 16:27:08.271:INFO:oejw.StandardDescriptorProcessor:NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
2014-02-22 16:27:08.319:WARN:oejs.Holder:
java.lang.ClassNotFoundException: ClientServlet
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:421)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:383)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:100)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:79)
at org.eclipse.jetty.servlet.Holder.doStart(Holder.java:107)
at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:298)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:791)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
at org.eclipse.jetty.server.Server.doStart(Server.java:282)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at com.jetty.RunJetty.main(RunJetty.java:24)
2014-02-22 16:27:08.321:WARN:/:unavailable

嵌入jetty服务器代码

    package com.jetty;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class RunJetty {

public RunJetty() {
}
public static void main(String[] args) {
Server server=new Server(8080);
WebAppContext wac = new WebAppContext();
wac.setResourceBase("C:/Users/User/j2ee/workspace/CLient1");
wac.setDescriptor("C:/Users/User/j2ee/workspace/CLient1/WebContent/WEB-INF/web.xml");
wac.setContextPath("/");
wac.setParentLoaderPriority(true);
server.setHandler(wac);
try {
server.start();
server.join();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Servlet 代码

    package com.jetty;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.RepositoryIdHelper;

public class ClientServlet extends HttpServlet {

private static final long serialVersionUID = 8120079003868426157L;
private String greeting="Hello World";

public ClientServlet(){}
public ClientServlet(String greeting)
{
this.greeting=greeting;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>"+greeting+"</h1>");

}

}

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>kajh</display-name>

<servlet>
<description></description>
<display-name>ClientServlet</display-name>
<servlet-name>ClientServlet</servlet-name>
<servlet-class>ClientServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ClientServlet</servlet-name>
<url-pattern>/ClientServlet</url-pattern>
</servlet-mapping>
</web-app>

此外,我想在我的servlet 中使用response.sendRedirect。比如,

      public class ClientServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 8120079003868426157L;
private String greeting="Hello World";

public ClientServlet(){}
public ClientServlet(String greeting)
{
this.greeting=greeting;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.sendRedirect("http://localhost:8080/CLient1/index.html");

}
}

请帮忙!我永远困在这个问题上。预先感谢您!

此外,我还添加了应用程序的文件夹结构,以便更好地理解

enter image description here

最佳答案

我认为 servlet-class 应该是

<servlet-class>com.jetty.ClientServlet</servlet-class>

编辑:使其也显示代码格式。

关于java - 爆发 war 中的嵌入式jetty服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21954522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com