Java-based data collection (final)

Source: Internet
Author: User

Java-based data collection (final)
(1) Capture page information and display (2) simple collection and storage (3) call local database query (4) Remote Call implementation (not implemented). These functions are based on local, sometimes we need to remotely call this type of data. In this case, we can use the RMI mechanism provided by JAVA to implement remote call access. Of course, you can also use WebServices implementation (PHP version, there is time to write a JAVA version): http://www.cnblogs.com/lichenwei/p/3891297.html what is RMI? RMI refers to Remote Method Invocation ). It is a mechanism that allows objects on a Java Virtual Machine to call methods on objects on another Java virtual machine. Any object that can be called using this method must implement this remote interface. When such an object is called, its parameter is "financialled" and it is sent from the local virtual machine to the remote virtual machine (the parameter of this remote virtual machine is "unexternalled. When this method is terminated, it groups the results from the remote machine and sends the results to the caller's virtual machine. If an exception is thrown due to a method call, the exception is indicated to the caller. Let's take a look at RMI. 1. Define a Remote interface. First, we need to write a Remote interface, IHello, which inherits the Remote Object Remote. the IHello Interface contains a hello method, which is used to greet the client after connection. because IHello inherits the Remote object, a RemoteException must be thrown. copy code 1 import java. rmi. remote; 2 import java. rmi. remoteException; 3 4 5 public interface IHello extends Remote {6 7 public String hello (String name) throws RemoteException; 8} copy code 2. Implement the interface. Next, we implement the methods in this interface, and the methods to implement the interface are on the server. the HelloImpl class implements the methods in the IHello interface. note: Here HelloImpl also inherits the UnicastRemoteObject remote object, which must be written. Otherwise, an error will be reported after the server is started. copy code 1 import java. rmi. remoteException; 2 import java. rmi. server. unicastRemoteObject; 3 4/** 5 * UnicastRemoteObject must be written. No error occurs even if no code is written, however, when running the server, an inexplicable error occurs. 6 * @ author Balla _ rabbit 7*8 */9 public class HelloImpl extends UnicastRemoteObject implements IHello {10 11 protected HelloImpl () throws RemoteException {12 super (); 13} 14 15 @ Overr Ide16 public String hello (String name) {17 String strHello = "hello! "+ Name +" accessing server "; 18 System. out. println (name + "accessing server"); 19 return strHello; 20} 21 22} copy code 3. Write the server. Because RMI implements remote access: the client finds the address (server address) of the remote interface object on the RMI registry to achieve remote access. Therefore, we need to create a remote object registry on the server, it is used to bind and register the server address and remote interface object, so that the client can successfully find the server to copy code 1 import java. rmi. naming; 2 import java. rmi. remoteException; 3 import java. rmi. registry. locateRegistry; 4 5 6 public class Server {7 8/** 9 * @ param args10 */11 public Static void main (String [] args) {12 try {13 IHello hello = new HelloImpl (); 14 int port = 6666; 15 LocateRegistry. createRegistry (port); 16 String address = "rmi: // localhost:" + port + "/tuzi"; 17 Naming. bind (address, hello); 18 System. out. println (">>> server started successfully"); 19 System. out. println (">>> please start the client to connect to access .. "); 20 21} catch (Exception e) {22 e. printStackTrace (); 23} 24} 25 26} copy Code 4. Write a Client client and define a remote access address, that is, the server address, Then, you can find the address in the RMI registry. if the address is found, a connection is established. copy code 1 import java.net. malformedURLException; 2 import java. rmi. naming; 3 import java. rmi. notBoundException; 4 import java. rmi. remoteException; 5 import java. util. secret; 6 7 8 public class Client {9 public static void main (String [] args) {10 11 int port = 6666; 12 String address = "rmi: // localhost: "+ port +"/tuzi "; 13 try {14 IHello hello = (IHello) Naming. lookup (address ); 15 System. out. println ("<client access successful! "); 16 // The Client calls the sayHello method in the remote interface and prints 17 System. out. println (hello. hello ("Rabbit"); 18 bytes = new bytes (System. in); 19 String input = inputs. next (); 20} catch (MalformedURLException e) {21 // TODO Auto-generated catch block22 e. printStackTrace (); 23} catch (RemoteException e) {24 // TODO Auto-generated catch block25 e. printStackTrace (); 26} catch (NotBoundException e) {27 // TODO Auto-generated catch block28 e. printStackTrace (); 29} 30 31} 32}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.