Java learning path-Hessian learning, java path-hessian

Source: Internet
Author: User

Java learning path-Hessian learning, java path-hessian


Hessian is a lightweight HTTP-based remote service solution. Hessian, like Rmi, uses binary messages for client-to-server interaction. However, unlike other binary remote calling technologies (such as Rmi), binary messages can be transplanted to other non-Java languages.
1. Four Steps for creating the Hessian Program
1. Define a remote interface.
2. Define a class to implement this interface.
3. Define the information required to export the Hessian service in web. xml.
4. Write client access code.
II. Specific implementation of the program
1. First, create a Web project and create an object class. This class must 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   }  9   public void setName(String name) { 10     this.name = name; 11   } 12   public double getPrice() { 13     return price; 14   } 15   public void setPrice(double price) { 16     this.price = price; 17   } 18 } 

2. Create an Interface

1 package service; 2 import java.util.List; 3 import entity.Book; 4 public interface BookService { 5   List<Book> getList(); 6 } 

3. Create a class to implement the interface in step 2, and this class needs to inherit the HessianServlet class (here we need the Hessian jar file to download the http://hessian.caucho.com/#Java to this website)

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> (); 10 Book b1 = new Book (); 11 b1.setName ("Brief History of information"); 12 b1.setPrice (56); 13 Book b2 = new Book (); 14 b2.setName ("hacker and painter"); 15 b2.setPrice (48); 16 list. add (b1); 17 list. add (b2); 18 return list; 19} 20 21}

4. Configure information in web. xml under the 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>                     

 


After configuration, deploy the project to the Tomcat server and start the service.

 
5. Write client access code

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/book "; 9 HessianProxyFactory factory = new HessianProxyFactory (); 10 try {11 BookService bookService = (BookService) factory. create (BookService. class , Url); 12 List <Book> list = bookService. getList (); 13 for (Book book: list) {14 System. out. println (book. getName () + ", pricing:" + book. getPrice () + "RMB. "); 15} 16} catch (Exception e) {17 e. printStackTrace (); 18} 19} 20}

 


Run the code. The console displays the result
============= Console =================

A Brief History of information. The price is 56.0 yuan.
Hacker and painter, priced at 48.0 yuan.

==================================

Next, let's talk about how Spring integrates Hessian.

Integrate Hessian with Spring

Note:

Hassian 3.2.0 adopts the new Hassian 2 protocol, while Spring2.5.6 only supports Hassian 1 Protocol. Therefore, the maximum version supported by spring 2.5.6 is Hassian 3.1.6. It is best to use Hassian 3.1.3, which is included in spring 2.5.6, for Hassian 2, Spring 3.0 is required.

1. First, create an interface

1 package service;2 import java.util.List;3 import entity.Book;4 public interface BookService {5     List<Book> getList();6 }

2. Compile a class and only implement the interface in step 1

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> list = new ArrayList <Book> (); 9 Book b1 = new Book (); 10 b1.setName ("Brief History of information"); 11 b1.setPrice (56); 12 Book b2 = new Book (); 13 b2.setName ("hacker and painter"); 14 b2.setPrice (48); 15 list. add (b1); 16 list. add (b2); 17 return list; 18} 19}

3. We need to configure SpringMVC information in web. xml under the WEB-INF (spring integration hessian needs to use 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>

4. Configure 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"    />

5. Now create a WEB-INF file under the springmvc-servlet.xml directory and configure it. (You can copy applicationContext. xml to the directory and 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>

 

6. Next, we should configure and obtain the bean information of the Service in the client application applicationContext. xml (I am writing it 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"    />

7. Now let's write the test code. Before running the following code, deploy the project to Tomcat and run 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 ClassPathXmlApplicationContext ("applicationContext. xml "); 10 BookService bookServ Ice = (BookService) cxt. getBean ("getBookService"); 11 List <Book> list = bookService. getList (); 12 for (Book book: list) {13 System. out. println (book. getName () + ", pricing:" + book. getPrice () + "RMB. "); 14} 15} 16 17}

 

Run the code. The console displays the result
============= Console =================

A Brief History of information. The price is 56.0 yuan.
Hacker and painter, priced at 48.0 yuan.

==================================

Now we have learned how to integrate hessian with spring.

 


JAVA learning path

Linux: the operating system (the same as winXP) has no essential relationship with java, but in the company, it is generally required to use common linux commands.
XML: Data Transmission Format. Learn more about Java Web.
Strut: One of the open-source Java Web frameworks, one of the essential technologies of Java Web

Spring: One of the open-source frameworks of javaweb, one of the essential technologies of javaweb
Hibernate: One of the open-source frameworks of javaweb, one of the essential technologies of javaweb

The above three constitute the most common enterprise-level development framework technology ssh, (each technology name starts with a letter)

SERVLET: One of the basic Java Web technologies, required. This is the foundation of ssh and must be learned well.
Jsp: javaweb display layer, required!
We recommend that you first learn java BASICS (such as interfaces, inheritance, and control statements) and then javaweb, otherwise, Java Web will learn servlet, jsp, and javabean before learning framework technology. In framework technology, I first learned struct2 spring

Java Web can be said to be used as a website, while javase is one of the major functions of java3 (j2s, J2ee, and javase ).
J2ee is enterprise-level development,
Java EE is standard development. j2ee and javase (also called j2se) are basically used for developing desktop applications and websites. However, the former has better functions than the latter.

How to Learn java

I don't know how your foundation works! It seems that you have no foundation. If so, I will share with you my own experiences.
At first, I heard about java from my classmates. I started the java electives when I was in the 3rd class, and I didn't take them well. After all, I only had a few weeks of classes. The teacher spoke very quickly, I have not learned anything! After I finished my graduation project, I went to learn java by myself and picked up the book to compile the simplest program for a long time. I almost gave up! I changed the policy.
Step 1: I read the thin book first.
Step 2: Compile the example code in the book and do exercises again. I found myself getting started!
Step 3: Find a subject, make small things, chat software on the LAN, calculator, notepad, and other things. That will make my classmates admire me very much. In fact, it is very easy to get into the door. It is difficult to get started.
Step 4: even if the above j2se is finished, we will learn java web development, jsp, struts, hibenate, and spring through video. In this process, we will exercise our programming skills through projects.
In fact, learning may not have such a strict limit. My method may not be the best, but it may be suitable for me and I will give you a reference.
When you can complete a student achievement management system independently, you apply for a job in the company. As a fresh graduate, your level is already high!
If you think my answer is helpful or helpful to you, please give me extra points. If you have any questions to discuss with me, Baidu, qq, and mobile phones can all help the Group's children who love learning!

Related Article

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.