Use hessian + protocol buffer + easyUI comprehensive case -- login, hessianeasyui

Source: Internet
Author: User

Use hessian + protocol buffer + easyUI comprehensive case -- login, hessianeasyui

First, we will briefly introduce hessian, protocol buffer, and easyUI framework.

Hessian:

Hessian is a lightweight remoting on http tool that uses the Binary RPC protocol. Therefore, Hessian is suitable for sending Binary data and has firewall penetration capabilities. Hessian generally provides services through Web applications, so it is very similar to the WebService we usually use. But it does not use the SOAP protocol, but is simpler and faster than webservice. Hessian Official Website: http://hessian.caucho.com/

Hessian can provide remote services through Servlet, and requests matching a certain pattern need to be mapped to Hessian service. You can also integrate the Spring framework and use its DispatcherServlet to complete this function. DispatcherServlet can forward requests in the matching mode to the Hessian service. Hessian server provides a servlet base class for processing sent requests. Hessian remote process calls are fully implemented using dynamic proxies. We recommend that you use interface-oriented programming, hessian service is exposed through interfaces.

Hessian processing process: client --> serialize to output stream --> remote method (server side) --> serialize to output stream --> client reads input stream --> output result

The package to be downloaded using hessian: hessian-4.0.37.jar;

Protocol buffer:

ProtocolBuffer(PB) is a data exchange format of google, which is independent of languages and platforms. Google provides implementation in three languages: java, c ++, and python. Each implementation includes the compiler and library files of the corresponding language. Because it is a binary format, it is much faster than using xml for data exchange. It can be used for data communication between distributed applications or data exchange in heterogeneous environments. As a binary data transmission format with excellent efficiency and compatibility, it can be used in many fields such as network transmission, configuration file, and data storage.

EasyUI:

JQuery EasyUI is a collection of jQuery-based UI plug-ins. The goal of jQuery EasyUI is to help web developers easily create a rich and beautiful UI interface. Developers do not need to write complex javascript or have a deep understanding of css styles. Developers only need to know some simple html tags.

The example is as follows:

Requirements:

The user account on Server 1 can log on to other applications (Server2) to achieve the purpose of multi-purpose Server 1 and user data sharing.

The main functions are as follows:

① User Login

② Display User Information

Logon Process

① The user accesses the login interface of Server 2 through a browser.

② Enter the account password and click Login.

③ Server2 calls the Account Verification interface of server1.

④ After Server1 verifies the account information (username and password) sent by Server2, return the verification result.

⑤ Server2 receives and processes the verification result returned by Server1, and then returns the relevant information to the user (prompting logon failure or Display User information ).

Technical Requirements

① All webpage interfaces are written in easyui.

② Communication between servers (Server1 and Server2) is based on protobuf and hessian (protobuf is used for data transmission and hessian is used for remote interface calls ).

③ The input parameters and return values of hessian remote interface methods are byte arrays.

When Server2 calls the Server1 interface, it constructs a protobuf object and fills in the serialized byte array of the object after filling the property to Server1. Server1 receives the request, deserializes the byte array passed by Server 2 into a protobuf object and obtains the attribute values (such as the user account and password). After processing, enter the processing result into the protobuf object, and return the serialization result (byte array) of the object ).

Flowchart:

Specific implementation:

First download the required package hessioan-4.0.37.jar, must install protocol buffer, download easyUI package

First, write the server:

Create a web project hessianServer

The directory is as follows:

Create the user. proto file under examples in the protocol installation directory

package com.hessian.model;option java_package="com.hessian.model";option java_outer_classname="UserProtos";message User{    required string name=1;    required string password=2;    required string birth=3;    optional string email = 4;    optional int64 phone=5;}

Enter the XXX \ protobuf-2.4.1 \ examples directory, you can see the user. proto file, execute the command protoc -- java_out =. user. proto command, if the com folder is generated and the UserProtos class is finally generated.

Import the protobuf-2.4.1 under the UserProtos. java and XXX \ protobuf-java-2.4.1.jar \ java \ target directories to a web project

Write the server code below

IService:

package com.hessian.service;public interface IService {        public Boolean login(byte[] user);    }

ServiceImpl

Package com. hessian. service. impl; import java. io. byteArrayInputStream; import java. io. IOException; import java. io. objectInputStream; import com. google. protobuf. invalidProtocolBufferException; import com. hessian. model. userProtos; import com. hessian. model. userProtos. user; import com. hessian. service. IService; public class ServiceImpl implements IService {public Boolean login (byte [] user) {UserProtos. user use = null; try {use = UserProtos. user. parseFrom (user); // convert the byte array to the object System. out. println (use);} catch (InvalidProtocolBufferException e) {// TODO Auto-generated catch block e. printStackTrace ();} // perform username and password verification if (use. getName (). equals ("oumyye") & use. getPassword (). equals ("oumyye") {return true;} else {return false ;}}}

Web. xml configuration

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> <servlet> <! -- Configure HessianServlet and configure the Servlet name as needed. For example, configure ServiceServlet --> <servlet-name> ServiceServlet </servlet-name> <servlet-class> com. caucho. hessian. server. hessianServlet </servlet-class> <! -- Configure the specific implementation class of the interface --> <init-param> <param-name> service-class </param-name> <param-value> com. hessian. service. impl. serviceImpl </param-value> </init-param> </servlet> <! -- Ing the access URL of HessianServlet --> <servlet-mapping> <servlet-name> ServiceServlet </servlet-name> <url-pattern>/ServiceServlet </url-pattern> </servlet-mapping> </web-app>

The service service1 has been compiled.

Http: // localhost: 8080/HessianServer/ServiceServlet appears

The Code under src is packaged into a hessian-common.jar file.

Write the loginClient for the service2 Client

First, import the relevant jar packages and directories as follows:

Compile the front-end code:

Index. jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! DOCTYPE html> 

Success. jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! DOCTYPE html> 

Then write the servlet code

Package com. hessian. servlet; import java. io. IOException; import javax. servlet. requestDispatcher; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com. caucho. hessian. client. hessianProxyFactory; import com. hessian. model. userProtos. user. builder; import com. hessian. service. IService; pu Blic class loginServlet extends HttpServlet {/*****/private static final long serialVersionUID = 1L; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String url = "http: // 192.168.2.108: 8 080/HessianServer/ServiceServlet "; HessianProxyFactory factory = new HessianProxyFactory (); IService service = (IService) factory. create (IService. class, url); // create the Instance Object com. hessian. model. userProtos. user. builder ump = com. hessian. model. userProtos. user. newBuilder (); ump. setName (request. getParameter ("name"); ump. setPassword (request. getParameter ("password"); ump. setEmail ("54654@qq.com"); ump. SetBirth ("19931223"); ump. setPhone (12313213); com. hessian. model. userProtos. user info = ump. build (); byte [] user = info. toByteArray (); if (service. login (user) {RequestDispatcher dispatcher = request. getRequestDispatcher ("success. jsp "); dispatcher. forward (request, response);} else {request. setAttribute ("info", "Login Failed! "); RequestDispatcher dispatcher = request. getRequestDispatcher (" index. jsp "); dispatcher. forward (request, response );}}}

Compile web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <servlet>    <servlet-name>Login</servlet-name>    <servlet-class>com.hessian.servlet.loginServlet</servlet-class>    </servlet>        <servlet-mapping>        <servlet-name>Login</servlet-name>        <url-pattern>/Login</url-pattern>        </servlet-mapping></web-app>

In this way, even if the entire code is completed, we will test it below:

Http: // localhost: 8080/loginClient/can be logged on. When the user name and password are oumyye, the logon is successful and the success. jsp page is displayed.

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.