首先先简单介绍下hessian ,protocol buffer, easyUI框架

hessian:

Hessian是一个轻量级的remoting on http工具,采用的是Binary RPC协议,所以它很适合于发送二进制数据,同时又具有防火墙穿透能力。Hessian一般是通过Web应用来提供服务,因此非常类似于平时我们用的 WebService。只是它不使用SOAP协议,但相比webservice而言更简单、快捷。Hessian官网:http://hessian.caucho.com/

Hessian 可通过Servlet提供远程服务,需要将匹配某个模式的请求映射到Hessian服务。也可Spring框架整合,通过它的 DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务。Hessian的server端提供一个servlet基类, 用来处理发送的请求,而Hessian的这个远程过程调用,完全使用动态代理来实现的,,建议采用面向接口编程,Hessian服务通过接口暴露。

Hessian处理过程示意图:客户端——>序列化写到输出流——>远程方法(服务器端)——>序列化写到输出流 ——>客户端读取输入流——>输出结果

使用hessian所要下载的包:hessian-4.0.37.jar

protocol buffer:

protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台。google 提供了三种语言的实现:java、c++ 和 python,每一种实现都包含了相应语言的编译器以及库文件。由于它是一种二进制的格式,比使用 xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。

EasyUI:

jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面。开发者不需要编写复杂的javascript,也不需要对css样式有深入的了解,开发者需要了解的只有一些简单的html标签。

案例如下:

需求介绍:

实现Server1上的用户账号可在其他应用(Server2)上登录的功能,达到一号多用和用户数据共享的目的。

主要功能点如下:

① 用户登录

② 显示用户信息

登录流程

①用户通过浏览器访问Server2的登陆界面。

②用户输入账号密码,点击登陆按钮。

③Server2收到用户的登录请求,调用Server1的账号验证接口。

④Server1验证Server2发送过来的账号信息(用户名、密码)后,返回验证结果。

⑤Server2收到并处理Server1返回的验证结果,再将相关信息返回给用户(提示登录失败或者显示用户信息)。

技术需求

①所有网页界面均采用easyui编写。

②服务器之间(Server1和Server2)的通信基于protobuf和 hessian(protobuf用于数据传输,hessian用于远程接口调用)。

③hessian远程接口方法的入参和返回值类型均为字节数组。

Server2调用Server1的接口时,先构造protobuf对象,将属性填充完毕后,将该对象序列化得到的字节数组填入接口方法传给Server1;Server1收到该请求,将Server2传过来的字节数组反序列化成protobuf对象,获取其中的属性值(比如用户帐号、密码),处理完成后,将处理结果填入protobuf对象,并返回该对象的序列化结果(字节数组)。

流程图:

具体实现:

先下载所必须的包 hessioan-4.0.37.jar,必须安装protocol buffer,下载easyUI包

首先先写服务端:

创建web项目hessianServer

目录如下:

在protocol安装目录下的examples下创建user.proto文件

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;
}

进入XXX\protobuf-2.4.1\examples目录,可以看到user.proto文件,执行命令 protoc --java_out=. user.proto 命令,如果生成com文件夹并在最终生成UserProtos类。

将UserProtos.java和XXX\protobuf-2.4.1\java\target目录下的protobuf-java-2.4.1.jar, hessioan-4.0.37.jar导入到web项目中

下面编写服务端代码

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); //将字节数组转化为对象
System.out.println(use); } catch (InvalidProtocolBufferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //进行用户名,密码验证
if(use.getName().equals("oumyye")&&use.getPassword().equals("oumyye")){
return true;
}
else {
return false;
}
} }

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>
<!-- 配置 HessianServlet,Servlet的名字随便配置,例如这里配置成ServiceServlet-->
<servlet-name>ServiceServlet</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <!-- 配置接口的具体实现类 -->
<init-param>
<param-name>service-class</param-name>
<param-value>com.hessian.service.impl.ServiceImpl</param-value>
</init-param>
</servlet>
<!-- 映射 HessianServlet的访问URL地址-->
<servlet-mapping>
<servlet-name>ServiceServlet</servlet-name>
<url-pattern>/ServiceServlet</url-pattern>
</servlet-mapping> </web-app>

到此服务service1编写完成

进入http://localhost:8080/HessianServer/ServiceServlet出现

则编写成功,将src下的代码打包成hessian-common.jar文件。

下面进行service2客户端的编写loginClient

首先也要导入相关jar包,及目录如下:

编写前端代码:

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>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ValidateBox - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="themes/icon.css">
<link rel="stylesheet" type="text/css" href="demo.css">
<style type="text/css">
input,textarea{
width:200px;
border:1px solid #ccc;
padding:2px;
}
</style>
<script type="text/javascript" src="jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery/jquery.easyui.min.js"></script>
</head>
<body>
<h2>登陆</h2>
${info}
<div>
<form action="Login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input class="easyui-validatebox" data-options="required:true,validType:'length[1,3]'" name="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input class="easyui-validatebox" data-options="validType:'password'" name="password"></td>
</tr>
</table>
<input type="submit" value="登陆" style="width: 50px">
</form>
</div>
</body>
</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>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ValidateBox - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="themes/icon.css">
<link rel="stylesheet" type="text/css" href="demo.css">
<style type="text/css">
input,textarea{
width:200px;
border:1px solid #ccc;
padding:2px;
}
</style>
<script type="text/javascript" src="jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery/jquery.easyui.min.js"></script>
</head>
<body>
<h2>登陆成功</h2> ${name} </body>
</html>

然后编写servlet代码

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; public class loginServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String url = "http://192.168.2.108:8080/HessianServer/ServiceServlet";
HessianProxyFactory factory = new HessianProxyFactory();
IService service = (IService) factory.create(IService.class, url);//创建IService接口的实例对象
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", "登陆失败!");
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher .forward(request, response);
}
} }

编写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>

这样整个代码就算编写完成了,下面进行测试:

http://localhost:8080/loginClient/ 可进行登陆,当用户名密码为oumyye时可登陆成功,跳转success.jsp页面

使用hessian+protocol buffer+easyUI综合案例--登陆的更多相关文章

  1. Protocol buffer的使用案例

    Protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了多种语言的实现:java.c#.c++.go 和 python,每一种实 ...

  2. Golang 使用Protocol Buffer 案例

    目录 1. 前言 2. Protobuf 简介 2.1 Protobuf 优点 2.2 Protobuf 缺点 2.3 Protobuf Golang 安装使用 3. Protobuf 通讯案例 3. ...

  3. Xml,Json,Hessian,Protocol Buffers序列化对比

    简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...

  4. Protocol buffer序列化及其在微信蓝牙协议中的应用

    Protocol buffer是Google出品的一种轻便高效的结构化数据存储格式,可对结构化数据进行序列化,并具有语言无关.平台无关等特点,在通信协议和数据存储等领域已经得到广泛的应用.目前其已经提 ...

  5. jquery-easyUI第二篇【综合案例】

    基于easyUI开发的一个综合案例模版 <%@ page language="java" pageEncoding="UTF-8"%> <!D ...

  6. 一看看懂Protocol Buffer(协议篇)

    前言 由于笔者业团队的业务对即时通讯服务有很大的依赖,春节结束后的第一天,红包没到,产品同学先到了,产品同学和我说要做一款IM,看到需求文档后和设计图后笔者大吃一斤 这不就是一个翻版的web qq吗? ...

  7. Angular路由与多视图综合案例

    Ajax请求存在的几个问题 (1)Ajax请求不会留下History 记录,会导致浏览器后退按钮失效 (2)用户无法直接通过URL进入应用中的指定页面(保存书签.链接分享给朋友) (3)Ajax对SE ...

  8. Java与C++进行系统间交互:Protocol Buffer

    在一次项目中,因笔者负责的java端应用需要与公司C++系统进行交互,公司选定Protocol Buffer方案,故简单的了解一下 有需要的可以看一下其他作者的文章,了解一下Protobuf: htt ...

  9. 企业级应用,如何实现服务化五(dubbo综合案例)

    这是企业级应用,如何实现服务化第五篇.在上一篇企业级应用,如何实现服务化四(基础环境准备)中.已经准备好了zookeeper注册中心,和dubbo管理控制台.这一篇通过一个综合案例,看一看在企业级应用 ...

随机推荐

  1. 两两组合覆盖测试用例设计工具:PICT

    两两组合覆盖测试用例设计工具:PICT 2016-08-31 目录 1 成对测试简介2 PICT使用  2.1 安装 PICT  2.2 使用PICT3 PICT算法  3.1 准备阶段  3.2 产 ...

  2. Leetcode 7 Reverse Integer 数论

    题意:将整数倒置,该题简单但是需要注意数据的范围,难得的好题. 如果出现1000000003或者-2000000003,倒置后的数超过int的范围,因此返回0,出现这种情况可以使用long long, ...

  3. jQuery单选框radio绑定click事件

    <div class="con_head"> <label><input type="radio" name="ask& ...

  4. GO語言視頻教程

    第1课:https://github.com/Unknwon/go-fundamental-programming/blob/master/lectures/lecture1.md Go开发环境搭建h ...

  5. Python操作Excel

    一.系统性学习 对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址: python操作Excel读写--使用xlrd 官方文档 Python 使用 Xlrd/xlwt 操 ...

  6. samba权限之easy举例说明--原创

    实验环境RHEL5.0,samba3.023rc-2 一.何为browsealbe=no? 如图lingdao目录的权限为777 如图ling目录的共享设置和用户的ID和组 当用户lingdao_01 ...

  7. CCNA实验4:HDLC和PPP

    一.HDLC封装 router9和11上分别配置s0/0如下 conf t int s0/0 encapsulation hdlc do show int s0/0 ip address x.x.x. ...

  8. 如何判断平台工具集去做条件编译(VC++目录、预处理器定义、$(PlatformToolsetVersion))

    作者:zyl910 从VS2010开始,提供了一个平台工作集(Platform ToolSet)选项用于配制vc编译版本.到了VS2012,更是因为默认平台工具集不支持WindowsXP,导致经常需要 ...

  9. PUT vs POST in REST

    来自:http://stackoverflow.com/questions/630453/put-vs-post-in-rest http://www.15yan.com/story/7dz6oXiS ...

  10. Linux内核同步方法

    1.原子操作,是其它同步方法的基础. 2.自旋锁,线程试图获取一个已经被别人持有的自旋锁,当前线程处于忙等待,占用cpu资源. 3.读写自旋锁,根据通用性和针对性的特点,普通自旋锁在特定场景下的表现会 ...