gpt4 book ai didi

eclipse - 检测运行的servlet容器是否是Eclipse Jetty

转载 作者:行者123 更新时间:2023-12-02 16:45:43 25 4
gpt4 key购买 nike

我尝试检测应用程序是否作为 servlet 容器在 Eclipse Jetty 上运行。我找到了 solution for Mortbay Jetty ,但不是 Eclipse 的实际版本。我修改了现有检查并将类路径从“/org/mortbay/jetty/Server.class”替换为“/org/eclipse/jetty/server/Server.class”,但检查在 Jetty 9 上失败。在tomcat成功了。我做错了什么?

代码如下:

private boolean detect(final String clazz) {
try {
final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
systemClassLoader.loadClass(clazz);
return true;
} catch (final ClassNotFoundException cnfe) {
final Class<?> classObj = getClass();
if (classObj.getResource(clazz) != null) {
return true;
} else {
return false;
}
}
}

调用函数:

detect("/org/eclipse/jetty/server/Server.class");

最佳答案

访问服务器类可以从嵌入式模式完成,但不能从独立的 Jetty 完成。这是因为 WebAppClassloader 将对 org.eclipse.jetty.server.Server 类的访问隔离到 Web 应用程序。

但是,您可以利用以下事实:所有 servlet 容器都必须具有 default servlet 的实现,只需使用 ServletContext访问RequestDispatcher对于默认的 servlet 并注意实现类名称。在嵌入式使用期间,似乎在 Jetty 8.1.10 和 9.0.3 上都运行良好。 (很快就会在独立 jetty 上再次尝试)

全类为例:

package com.company.foo;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = { "/detect" })
@SuppressWarnings("serial")
public class DetectServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
detect(out,"org.eclipse.jetty.server.Server");
detect(out,"/org/eclipse/jetty/server/Server.class");
detect(out,"org.eclipse.jetty.server.Request");
detect(out,"/org/eclipse/jetty/server/Request.class");
detect(out,"org.mortbay.jetty.Server");
detect(out,"/org/mortbay/jetty/Server.class");

detectDefaultServlet(out, req, "org.eclipse.jetty");
detectDefaultServlet(out, req, "org.apache.catalina");

String version = getReflectedMethodValue("org.eclipse.jetty.server.Server", "getVersion");
out.printf("Server.getVersion() = %s%n", version);
}

private void detectDefaultServlet(PrintWriter out, HttpServletRequest req, String keyword)
{
// Request the default servlet (its pretty safe to say it will always be there)
RequestDispatcher dispatcher = req.getServletContext().getNamedDispatcher("default");
if(dispatcher == null) {
out.printf("detectDefaultServlet(out, req, \"%s\") = <no default servlet>%n", keyword);
return;
}

// If the request dispatcher implementation contains the keyword, we can claim a match
boolean detected = dispatcher.getClass().getName().contains(keyword);
out.printf("detectDefaultServlet(out, req, \"%s\") = %b (%s)%n", keyword, detected, dispatcher.getClass().getName());
}

private String getReflectedMethodValue(String clazzName, String methodName)
{
ClassLoader loader = ClassLoader.getSystemClassLoader();
try
{
Class<?> clazz = Class.forName(clazzName,false,loader);
Class<?> parameterTypes[] = new Class<?>[0];
Method method = clazz.getDeclaredMethod(methodName,parameterTypes);
Object args[] = new Object[0];
return (String)method.invoke(clazz,args);
}
catch (ClassNotFoundException e)
{
return "<class-not-found>";
}
catch (Throwable t)
{
return "<" + t.getClass().getName() + ": " + t.getMessage() + ">";
}
}

private void detect(PrintWriter out, String resource)
{
out.printf("detect(\"%s\") = %b%n",resource,detected(resource));
}

private boolean detected(String clazz)
{
try
{
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();

systemClassLoader.loadClass(clazz);

return true;
}
catch (ClassNotFoundException cnfe)
{
Class<?> classObj = getClass();

if (classObj.getResource(clazz) != null)
{
return true;
}
else
{
return false;
}
}
}
}

这将在 Jetty 8.1.10 嵌入式上产生以下输出

detect("org.eclipse.jetty.server.Server") = true
detect("/org/eclipse/jetty/server/Server.class") = true
detect("org.eclipse.jetty.server.Request") = true
detect("/org/eclipse/jetty/server/Request.class") = true
detect("org.mortbay.jetty.Server") = false
detect("/org/mortbay/jetty/Server.class") = false
detectDefaultServlet(out, req, "org.eclipse.jetty") = true (org.eclipse.jetty.server.Dispatcher)
detectDefaultServlet(out, req, "org.apache.catalina") = false (org.eclipse.jetty.server.Dispatcher)
Server.getVersion() = 8.1.10.v20130312

Jetty 9.0.3 上的类似输出嵌入式

detect("org.eclipse.jetty.server.Server") = true
detect("/org/eclipse/jetty/server/Server.class") = true
detect("org.eclipse.jetty.server.Request") = true
detect("/org/eclipse/jetty/server/Request.class") = true
detect("org.mortbay.jetty.Server") = false
detect("/org/mortbay/jetty/Server.class") = false
detectDefaultServlet(out, req, "org.eclipse.jetty") = true (org.eclipse.jetty.server.Dispatcher)
detectDefaultServlet(out, req, "org.apache.catalina") = false (org.eclipse.jetty.server.Dispatcher)
Server.getVersion() = 9.0.3.v20130506

但 Jetty 9.0.3 有所不同独立版

detect("org.eclipse.jetty.server.Server") = false
detect("/org/eclipse/jetty/server/Server.class") = false
detect("org.eclipse.jetty.server.Request") = false
detect("/org/eclipse/jetty/server/Request.class") = false
detect("org.mortbay.jetty.Server") = false
detect("/org/mortbay/jetty/Server.class") = false
detectDefaultServlet(out, req, "org.eclipse.jetty") = true (org.eclipse.jetty.server.Dispatcher)
detectDefaultServlet(out, req, "org.apache.catalina") = false (org.eclipse.jetty.server.Dispatcher)
Server.getVersion() = <class-not-found>

关于eclipse - 检测运行的servlet容器是否是Eclipse Jetty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17151551/

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