从Applet调用本地服务

时间:2012-12-10 23:24:53

标签: java web-services applet policy wsimport

从我阅读的所有内容中,applet可以调用自己服务器提供的任何服务。这就是为什么我写和启动服务(在netbeans中),我可以在我的浏览器中看到它

http://localhost:12345/server?wsdl

要连接到我使用wsimport的服务,请将文件放在我的applet目录中,然后我清理并构建项目。

如果我同时启动(Service + Applet)它工作正常,当我删除策略文件时,它不会。

Caused by: java.security.AccessControlException: access denied                       
("java.util.PropertyPermission" "xml.catalog.ignoreMissing" "read")

我在Netbeans中启动了服务并将applet放在我的本地服务器上(xampp) 我可以通过

访问它
http://localhost/sql/mainapplet.html

但没有任何反应。我做错了什么?

这是代码:

服务器项目:

CP.java

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService(targetNamespace = "GUI")
@SOAPBinding(style = Style.RPC)
public class CP {
public String getResults(String u, String p, String s, String se, String q) {
  ...
 }
}

Server.java

public class Server {
public static void main(String[] args) {
    CP server = new CP();
    Endpoint e = Endpoint.publish("http://localhost:12345/server", server);
 }
}

客户项目: CP.java

package gui;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 * 
 */
@WebService(name = "CP", targetNamespace = "GUI")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface CP {


/**
 * 
 * @param arg4
 * @param arg3
 * @param arg2
 * @param arg1
 * @param arg0
 * @return
 *     returns java.lang.String
 */
@WebMethod
@WebResult(partName = "return")
@Action(input = "GUI/CP/getResultsRequest", output = "GUI/CP/getResultsResponse")
public String getResults(
    @WebParam(name = "arg0", partName = "arg0")
    String arg0,
    @WebParam(name = "arg1", partName = "arg1")
    String arg1,
    @WebParam(name = "arg2", partName = "arg2")
    String arg2,
    @WebParam(name = "arg3", partName = "arg3")
    String arg3,
    @WebParam(name = "arg4", partName = "arg4")
    String arg4);

}

CPService.java

package gui;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "CPService", targetNamespace = "GUI", wsdlLocation =     "http://localhost:12345/server?wsdl")
public class CPService
extends Service {

private final static URL CPSERVICE_WSDL_LOCATION;
private final static WebServiceException CPSERVICE_EXCEPTION;
private final static QName CPSERVICE_QNAME = new QName("GUI", "CPService");

static {
    URL url = null;
    WebServiceException e = null;
    try {
        url = new URL("http://localhost:12345/server?wsdl");
    } catch (MalformedURLException ex) {
        e = new WebServiceException(ex);
    }
    CPSERVICE_WSDL_LOCATION = url;
    CPSERVICE_EXCEPTION = e;
}

public CPService() {
    super(__getWsdlLocation(), CPSERVICE_QNAME);
}

public CPService(WebServiceFeature... features) {
    super(__getWsdlLocation(), CPSERVICE_QNAME, features);
}

public CPService(URL wsdlLocation) {
    super(wsdlLocation, CPSERVICE_QNAME);
}

public CPService(URL wsdlLocation, WebServiceFeature... features) {
    super(wsdlLocation, CPSERVICE_QNAME, features);
}

public CPService(URL wsdlLocation, QName serviceName) {
    super(wsdlLocation, serviceName);
}

public CPService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
    super(wsdlLocation, serviceName, features);
}

/**
 * 
 * @return
 *     returns CP
 */
@WebEndpoint(name = "CPPort")
public CP getCPPort() {
    return super.getPort(new QName("GUI", "CPPort"), CP.class);
}

/**
 * 
 * @param features
 *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
 * @return
 *     returns CP
 */
@WebEndpoint(name = "CPPort")
public CP getCPPort(WebServiceFeature... features) {
    return super.getPort(new QName("GUI", "CPPort"), CP.class, features);
}

private static URL __getWsdlLocation() {
    if (CPSERVICE_EXCEPTION!= null) {
        throw CPSERVICE_EXCEPTION;
    }
    return CPSERVICE_WSDL_LOCATION;
 }

}

MainApplet.java

package gui;

public class MainApplet extends javax.swing.JApplet {
...
    CPService cps = new CPService();
    CP cproc = cps.getCPPort();
    txtResult.setText(cproc.getResults("a","b","j","s", "q"));
}

HTML的文件:

<HTML>
<BODY>
<P>
<APPLET codebase="client/build/classes" code="gui/MainApplet.class"     archive="client/dist/client.jar" width=1400 height=800></APPLET>
</P>
</BODY>
</HTML>

1 个答案:

答案 0 :(得分:0)

问题解决了:我不得不像这样嵌入applet

<applet archive="client.jar" code="gui.MainApplet" width="1600" height="1400"></applet>

我必须签署我的小程序(仍不确定原因)。