最近,我使用javax.servlet包制作了一个Eclipse RAP程序,以创建文件下载服务。 当在Eclipse IDE中使用内置服务器时,它可以很好地工作。 但是,在Tomcat服务器上部署程序后,打开下载链接时,错误消息显示HTTP 500错误,如下所示:
类型:异常报告
消息:Servlet执行引发异常 说明服务器遇到意外状况,阻止其满足请求。
异常:javax.servlet.ServletException:Servlet执行引发异常 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因: java.lang.LinkageError:加载器约束违规:加载器(实例 org / eclipse / osgi / internal / loader / EquinoxClassLoader)之前 启动了具有名称的其他类型的加载 “ javax / servlet / http / HttpServletResponse” testproject.DownloadServiceHandler.service(DownloadServiceHandler.java:59) org.eclipse.rap.rwt.engine.RWTServlet.handleValidRequest(RWTServlet.java:135) org.eclipse.rap.rwt.engine.RWTServlet.handleRequest(RWTServlet.java:117) org.eclipse.rap.rwt.engine.RWTServlet.doGet(RWTServlet.java:100) javax.servlet.http.HttpServlet.service(HttpServlet.java:635) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.eclipse.rap.rwt.osgi.internal.CutOffContextPathWrapper.service(CutOffContextPathWrapper.java:106) org.eclipse.equinox.http.servlet.internal.HttpServiceRuntimeImpl $ LegacyServlet.service(HttpServiceRuntimeImpl.java:1240) org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:151) org.eclipse.equinox.http.servlet.internal.servlet.ResponseStateHandler.processRequest(ResponseStateHandler.java:65) org.eclipse.equinox.http.servlet.internal.context.DispatchTargets.doDispatch(DispatchTargets.java:134) org.eclipse.equinox.http.servlet.internal.servlet.ProxyServlet.service(ProxyServlet.java:103) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.eclipse.equinox.servletbridge.BridgeServlet.service(BridgeServlet.java:158) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
以下是我的代码和相关文件:
BasicApplication.java
package testproject;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.rap.rwt.application.Application;
import org.eclipse.rap.rwt.application.ApplicationConfiguration;
import org.eclipse.rap.rwt.client.WebClient;
public class BasicApplication implements ApplicationConfiguration {
public void configure(Application application) {
Map<String, String> properties = new HashMap<String, String>();
properties.put(WebClient.PAGE_TITLE, "Test Project");
application.addEntryPoint("/", BasicEntryPoint.class, properties);
}
}
BasicEntryPoint.java
package testproject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.application.AbstractEntryPoint;
import org.eclipse.rap.rwt.client.service.UrlLauncher;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
public class BasicEntryPoint extends AbstractEntryPoint {
@Override
protected void createContents(Composite parent) {
parent.setLayout(new GridLayout(2, false));
Button downloadButton = new Button(parent, SWT.PUSH);
downloadButton.setText("Download");
downloadButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
File testfile = new File("/home/user/Desktop/test.file");
byte[] fileContent = Files.readAllBytes(testfile.toPath());
sendDownload(fileContent, testfile.getName());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public boolean sendDownload(byte[] data, String filename) {
DownloadServiceHandler service = new DownloadServiceHandler(data, filename);
service.register();
UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
launcher.openURL(service.getURL());
return true;
}
}
DownloadServiceHandler.java
package testproject;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.service.ServiceHandler;
public class DownloadServiceHandler implements ServiceHandler {
private final byte[] data;
private final String filename;
private String id;
public DownloadServiceHandler(byte[] data, String filename) {
this.data = data;
this.filename = filename;
this.id = calculateId();
}
public String getURL() {
return RWT.getServiceManager().getServiceHandlerUrl(getId());
}
private String getId() {
return id;
}
private String calculateId() {
return String.valueOf(System.currentTimeMillis()) + data.length;
}
public boolean register() {
try {
RWT.getServiceManager().registerServiceHandler(getId(), this);
return true;
} catch (Exception e) {
System.out.println("failed to register download service handler");
return false;
}
}
private boolean unregister() {
try {
RWT.getServiceManager().unregisterServiceHandler(getId());
return true;
} catch (Exception e) {
System.out.println("failed to unregister download service handler");
return false;
}
}
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
response.setContentType("application/octet-stream"); // this is the error line "DownloadServiceHandler.java:59"
response.setContentLength(data.length);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename
+ "\"");
response.getOutputStream().write(data);
} catch (Exception e) {
System.out.println("failed to dispatch download");
} finally {
unregister();
}
}
}
META-INF / MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Testproject
Bundle-SymbolicName: testproject
Bundle-Version: 1.0.0.qualifier
Automatic-Module-Name: testproject
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.eclipse.rap.rwt;bundle-version="[3.0.0,4.0.0)"
Service-Component: OSGI-INF/contribution.xml
Import-Package: javax.servlet;version="3.1.0",
javax.servlet.http;version="3.1.0"
Bundle-ClassPath: .
OSGI-INF / contribution.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="BasicApplication">
<implementation class="testproject.BasicApplication"/>
<service>
<provide interface="org.eclipse.rap.rwt.application.ApplicationConfiguration"/>
</service>
</scr:component>
我应该如何解决此错误?我不知道它如何在Eclipse IDE中工作,但是在Tomcat服务器上部署后却无法工作。 预先感谢。
答案 0 :(得分:0)
我自己找到了解决方法。...
我仅通过使用javax.servlet版本3.1.0限制了我的项目。也许不支持我安装的Tomcat服务器的版本。我对此不确定,但我在META-INF / MANIFEST.MF中修改了以下部分,此问题已解决。
导入包:javax.servlet; version =“ [2.3.0,4.0.0)”, javax.servlet.http; version =“ [2.3.0,4.0.0)”