eclipse rap下载服务servlet报错
eclipse rap download service servlet error
最近,我使用javax.servlet 包制作了一个Eclipse RAP 程序来创建一个文件下载服务。
在 Eclipse IDE 中使用内置服务器时,它工作得很好。
但是,在 Tomcat 服务器上部署程序后,在打开下载 link 时,错误消息显示 HTTP 500 错误如下:
Type: Exception Report
Message: Servlet execution threw an exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception: javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause:
java.lang.LinkageError: loader constraint violation: loader (instance
of org/eclipse/osgi/internal/loader/EquinoxClassLoader) previously
initiated loading for a different type with name
"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 服务器上部署后却无法工作。
提前致谢。
我自己找到了解决方法....
我通过仅使用 javax.servlet 版本 3.1.0 来限制我的项目。可能是我安装的Tomcat服务器版本不支持。我不确定这一点,但我修改了 META-INF/MANIFEST.MF 中的以下部分,问题已解决。
Import-Package: javax.servlet;version="[2.3.0,4.0.0)",
javax.servlet.http;version="[2.3.0,4.0.0)"
最近,我使用javax.servlet 包制作了一个Eclipse RAP 程序来创建一个文件下载服务。 在 Eclipse IDE 中使用内置服务器时,它工作得很好。 但是,在 Tomcat 服务器上部署程序后,在打开下载 link 时,错误消息显示 HTTP 500 错误如下:
Type: Exception Report
Message: Servlet execution threw an exception Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception: javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause: java.lang.LinkageError: loader constraint violation: loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) previously initiated loading for a different type with name "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 服务器上部署后却无法工作。 提前致谢。
我自己找到了解决方法....
我通过仅使用 javax.servlet 版本 3.1.0 来限制我的项目。可能是我安装的Tomcat服务器版本不支持。我不确定这一点,但我修改了 META-INF/MANIFEST.MF 中的以下部分,问题已解决。
Import-Package: javax.servlet;version="[2.3.0,4.0.0)", javax.servlet.http;version="[2.3.0,4.0.0)"