45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:怎么样在Web applications间共享数据

怎么样在Web applications间共享数据

2016-09-04 16:57:36 来源:www.45fan.com 【

怎么样在Web applications间共享数据

根据 ClassLoader 的装载顺序,首先装载那些最基础的类,然后才轮到web app中的类,而且基础类装载以后可由多个web app使用,因此,可以设想,如果我们把存储共享数据的util类打包与基础类放在一起,如classpath, 应用服务器的包文件存放处(这里使用的是tomcat,因此放在common/lib/下),就可以在各个web app中向这个类填充数据或者读取数据。

为什么这里说的是向类填充或读取数据呢?因为如果创建这个类的对象,势必这个对象要处于某个web app的context中,其它的web app必然无法访问到这个web app的context。那么怎么解决呢? 既然这些web app能使用到这个类,那么如果我们把这个类的所有的数据和方法都设置为static,那么自然web app们就可以直接访问到这些数据和方法,从而达到共享之目的。

经过我的测试,这样的共享是完全没问题的,只是要注意一下并发访问的情况。

基础类:
================================
package org.openthoughts.util;

import Java .util.HashMap;

public class ShareableRepository {
private static Map shared = new HashMap();


private ShareableRepository() {

}


public static Object get(String key) {
Object value = null;
synchronized(shared) {
value = shared.get(key)
}
return value;
}

public static void put(String key, Object value) {
synchronized(shared) {
shared.put(key, value);
}
}
}

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

第一个JSP:
=================================
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="org.openthoughts.util.ShareableRepository" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h1 align="center">You use this form to insert values to shared repository.</h1>

<%
String key = request.getParameter("key");
String value = request.getParameter("value");

if(null != key && value != null) {
ShareableRepository.put(key, value);
}
%>
<form action="index.jsp" method="POST">
<label for="key">Key:</label>
<input type="text" name="key" value="" /><br>
<label for="value">Value:</label>
<input type="text" name="value" value="" /><br>
<input type="submit" value="Submit" name="submit" />
</form>
</body>
</html>
====================================


第二个JSP:
====================================
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="org.openthoughts.util.ShareableRepository" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h1 align="center">You use this form to get values from shared repository.</h1>

<form action="index.jsp" method="POST">
<label for="key">Key:</label>
<input type="text" name="key" value="" /><br>
<input type="submit" value="Submit" name="submit" />
</form>

<%
String key = request.getParameter("key");

String value = "";
if(null != key) {
value = (String)ShareableRepository.get(key);
}
%>
The value you want to fetch is <%=value==null?"":value%>
</body>
</html>
=====================================

All these files are powerd by NetBeans IDE.

 

本文地址:http://www.45fan.com/a/question/72331.html
Tags: 数据 Web applications
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部