我从一个名为 PoliceDepartment.java 的 java 类创建了一个 Web 服务。我是这样做的:
Ctrl-n -> Web 服务 -> 服务器实现 -> 浏览至源代码 -> 完成。
我已经创建了一个 tomcat 服务器及其正在运行的所有内容。
即使面临死亡威胁,我也无法创建客户端。
我已经准备好客户端代码(见下文)。
Ctrl-n -> Web 服务 -> 将 wsdl 文档传递给它。
Eclipse 实际上试图覆盖 PoliceDepartment.java 类。
在任何情况下,它都不会创建 Web 客户端。
我需要的是简单地发送一个带有参数的信号:createClient("blabla")。
我想一定有一些工具可以创建到 PoliceDepartment 服务器的代理,我可以简单地从我的 java 客户端调用它。
如果你知道如何,请帮助我实现这一目标。
感谢您提前提供帮助。
这是来自命令行的“客户端”解析参数(它需要一个代理来将解析的参数发送到 Web 服务):
package client;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.regex.Pattern;
import interfaces.Compute;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Console {
public void loop(){
Scanner sc = new Scanner(System.in);
while(true){
System.err.println("Enter you badge id in digits");
String id = sc.nextLine();
System.err.println("Choose police station: SPVM, Brossard or Longueil");
String station = sc.nextLine();
if(station.equals("SPVM")||station.equals("Brossard")||station.equals("Longueil")){
PoliceOfficer popo = new PoliceOfficer(station, id);
Compute stub = (Compute) this.connect(popo.station);
while (true){
System.err.println("Choose which record you want: Criminal, Missing, Edit, Get Count; type Show to see database, Transfer to transfer a record or Exit to exit:");
try{
String choice=sc.nextLine();
if(choice.equals("Criminal")){
System.out.println("Please enter: firstname, lastname, description, status");
String firstname=sc.nextLine();
String lastname=sc.nextLine();
String description=sc.nextLine();
String status=sc.nextLine();
this.write(stub.createCRecord(firstname, lastname, description, status, popo.badgeID), popo.badgeID);
}else if(choice.equals("Missing")){
System.err.println("Please enter: firstname, lastname,address,last location, last date seen, status");
String firstname=sc.nextLine();
String lastname=sc.nextLine();
String address=sc.nextLine();
String lastlocation=sc.nextLine();
String lastdate = sc.nextLine();
String date[] = lastdate.split("-");
Date cal = new Date(Integer.parseInt(date[0])-1901,
Integer.parseInt(date[1]), Integer.parseInt(date[2]));
String status=sc.nextLine();
this.write(stub.createMRecord(firstname, lastname, address, cal, lastlocation, status, popo.badgeID), popo.badgeID);
} else if(choice.equals("Edit")){
System.err.println("Please enter: lastname, recordid number (only the digits), status");
String lastname=sc.nextLine();
int recordid=0;
try{
recordid=Integer.parseInt(sc.nextLine());
String newstatus=sc.nextLine();
this.write(stub.editCRecord(lastname, recordid, newstatus, popo.badgeID), popo.badgeID);
}catch (NumberFormatException e){
System.err.println("NOT INTEGER. ABORTING. RECORDID MUST BE INTEGER e.g CR0 you must enter 0");
}
} else if(choice.equals("Get Count")){
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
sendData="COUNT".getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
this.write("Get Count:" + modifiedSentence + "\n", popo.badgeID);
} else if(choice.equals("Show")){
this.write(stub.show(), popo.badgeID);
}else if(choice.equals("Transfer")){
System.err.println("Please enter the department and the id of the record");
String department=sc.nextLine();
String record_id=sc.nextLine();
System.err.println("Following record transfered to the "+department+" department:");
this.write(stub.transferRecord(department, record_id, popo.badgeID), popo.badgeID);
}
else if(choice.equals("Exit")){
break;
}
System.err.println("\n\n\n");
}catch(Exception e){
e.printStackTrace();
}
}//end inner while
}// end if the choice is SPVM, Longueil or Brossard
}//end outer while (choose police station)
}
public Compute connect(String station){
Compute stub=null;
try{
Registry registry = LocateRegistry.getRegistry();
stub = (Compute) registry.lookup(station);
}catch(Exception e){
System.err.println("an error in the client has occured");
e.printStackTrace();
}
return stub;
}
private void write(String s, String filename){
PrintWriter file=null;
try{
file = new PrintWriter(new FileOutputStream(
new File(filename),
true /* append = true */));
file.println(s);
System.err.println(s);
}catch(Exception e){
e.printStackTrace();
}finally{
file.close();
}
}
}