Hessian is a lightweight, HTTP-based remote service solution that Hessian, like RMI, uses binary messages for client and server-side interaction. But unlike other binary remote invocation techniques (such as RMI), its binary messages can be ported to other non-Java languages.
First, 4 steps to create the Hessian program
1. Define the interface of a remote interface.
2. Define a class that implements the interface.
3. In Web. XML, define the information required to export the Hessian service.
4, write the client access code.
Second, the specific implementation of the procedure
First, we create a Web project first, and a new entity class, this class needs to implement the serializable interface.
1 package entity; 2 import java.io.Serializable; 3 public class Book implements Serializable { 4 private String name; 5 private double price; 6 Public String GetName () { 7 return name; 8}
Second, create an interface
Third, create a class to implement the interface in step two, and this class needs to inherit the Hessianservlet class (here the Hessian jar file can be downloaded to this site http://hessian.caucho.com/#Java)
1 package Service.impl; 2 import java.util.ArrayList; 3 Import java.util.List; 4 Import Service. Bookservice; 5 import Com.caucho.hessian.server.HessianServlet; 6 Import entity. Book; 7 public class Bookserviceimpl extends Hessianservlet implements Bookservice { 8 public list<book> getList () { 9 list<book> list=new arraylist<book> (); b1=new Book (); B1.setname ("the A Brief History of Information "); ( b1.setprice), b2=new Book (), b2.setname ("Hacker and Painter"), B2.setprice (48); 16 List.add (B1); List.add (B2); return list; 19
Iv. configuration information in Web. XML under Web-inf
<servlet> <servlet-name>book</servlet-name> <servlet-class> com.caucho.hessian.server.hessianservlet</servlet-class> <init-param> <param-name> Home-api</param-name> <param-value>service. bookservice</param-value> </init-param> <init-param> <param-name> home-class</param-name> <param-value>service.impl.BookServiceImpl</param-value> < /init-param> </servlet> <servlet-mapping> <servlet-name>book</servlet-name> <url-pattern>/book</url-pattern> </servlet-mapping>
Once configured, deploy the project to the Tomcat server and start the service.
V. Writing Customer access codes
1 package test; 2 import java.util.List; 3 Import Service. Bookservice; 4 Import com.caucho.hessian.client.HessianProxyFactory; 5 Import entity. Book; 6 public class Test { 7 public static void Main (string[] args) { 8 String url= "Http://127.0.0.1:8080/test/bo OK ";
Run the code and the console displays the result as
=========== Console ============
"A Brief History of information", the price is: 56.0 yuan.
"Hacker and painter", The price is: 48.0 yuan.
=============================
Let's take a look at Spring integration Hessian
Spring Integrated Hessian
Precautions:
Hassian 3.2.0 uses the new Hassian 2 protocol, and Spring2.5.6 only supports the Hassian 1 protocol, so spring 2.5.6 can support the largest version of Hassian 3.1.6, preferably using spring 2.5.6 comes with version Hassian 3.1.3, and support for Hassian 2 requires Spring 3.0.
First, we create an interface
1 package service;2 import java.util.list;3 import entity. Book;4 public interface Bookservice {5 list<book> getList (); 6}
Second, write a class, just implement the interface in step one
1 package Service.impl; 2 Import java.util.ArrayList; 3 Import java.util.List; 4 Import Service. Bookservice; 5 Import entity. Book; 6 public class Bookserviceimpl implements Bookservice {7 public list<book> GetList () {8 list<book> l Ist=new arraylist<book> (); 9 Book B1=new Book (), b1.setname ("A Brief History of Information"), B1.setprice ( a); B2.setname ("The Hacker and the Painter"); ( b2.setprice); List.add (B1); B2 List.add (}19);
Third, we configure SPRINGMVC required information in Web. XML under Web-inf (Spring Integration Hessian need SPRINGMVC)
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationcontext.xml</param-value> </context-param> <listener> < Listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </ listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class >org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> < servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</ Url-pattern> </servlet-mapping>
Iv. Configuring bean information in Applicationcontext.xml
<bean id= "Bookservice" class= "Service.impl.BookServiceImpl" ></bean> <bean id= "Bookservice" class= "Org.springframework.remoting.caucho.HessianServiceExporter" p:service-ref= "Bookservice" p: Serviceinterface= "service. Bookservice " />
V. Now create a new Springmvc-servlet.xml file in the Web-inf directory and configure it. (You can copy the Applicationcontext.xml to the directory to change the name)
<bean id= "urlmapping" class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > < Property name= "Mappings" > <props> <prop key= "/book" >BookService</prop> </ props> </property> </bean>
Six, then we should configure the Get Service bean information in the client program Applicationcontext.xml (I am writing in the same Applicationcontext.xml file, but it does not affect the test function)
<bean id= "Getbookservice" class= "Org.springframework.remoting.caucho.HessianProxyFactoryBean" p: Serviceurl= "Http://127.0.0.1:8080/test/book" p:serviceinterface= "service. Bookservice " />
Now let's write the test code, deploy the project to tomcat before running the code below, and run the Tomcat
1 package test; 2 Import java.util.List; 3 Import Org.springframework.context.ApplicationContext; 4 Import Org.springframework.context.support.ClassPathXmlApplicationContext; 5 Import Service. Bookservice; 6 Import entity. Book; 7 public class Test {8 public static void Main (string[] args) {9 applicationcontext cxt=new classpathxmlapplicat Ioncontext ("Applicationcontext.xml"), bookservice bookservice= (bookservice) Cxt.getbean ("GetBookService"); list<book> List = Bookservice.getlist (); (book book:list) { System.out.println ( Book.getname () + ", Price is:" +book.getprice () + "Yuan. "); }15 }16 17}
Run the code and the console displays the result as
=========== Console ============
"A Brief History of information", the price is: 56.0 yuan.
"Hacker and painter", The price is: 48.0 yuan.
=============================
Here we have learned about spring and how to integrate Hessian.
Java Learning Path-hessian Learning