Monthly Archives: 7月 2016

Java開発EL(Expression Language)式を利用してMAP Listの値を取得する

1.EL式–MAP

1.1 actionコード:
Map map = new HashMap();
map.put(key1,value1);
map.put(key2,value2);
map.put(key3,value3);

1.2 JSP画面
<forEachitems=”${map}” var=”item”>
<c:outvalue=”${item.key}”/>
<c:outvalue=”${item.value}”/>
</forEach>

2.EL式–List
2.1 actionコード
List list = new ArrayList();
list.add(“study”);
list.add(“map skill”);
list.add(“welcome”);
list.add(“startnews24”);

書き方2
List<Map<String,Object>> employees = new ArrayList<Map<String,Object>>();
Map<String,Object> s1 = new HashMap<String,Object>();
s1.put(“name”,”yamada”);
s1.put(“age”,”35″);
employees.add(s1);
Map<String,Object> s2= new HashMap<String,Object>();
s2.put(“name”,”oohara”);
s2.put(“age”,”32″);
employees.add(s2);

2.2 サンプルコード
<c:forEach var =”employee” items =”$ {employees}” >
<c:out value=”$ {employee.name}” default=”wang”/> </td>
<c:out value=”$ {employee.age}” default=”wang”/>
</c:forEach>

JavaScriptにjsonにシリアライズするサンプルコード

参考コード:
SerializeJsonToStr : function( objectJson )
{
if( objectJson == null )
return “null”;
if( typeof(objectJson) == typeof(0) )
return objectJson.toString();
if( typeof(objectJson) == typeof(”) ||
objectJson instanceof String )
{
objectJson = objectJson.toString();
objectJson = objectJson.replace( //r/n/, ‘//r//n’);
objectJson = objectJson.replace( //n/, ‘//n’);
objectJson = objectJson.replace( //”/, ‘//”‘);
return ‘”‘ + objectJson + ‘”‘;
}
if( objectJson instanceof Array )
{
var strRet = “[“;
for( var i = 0; i < objectJson.length; i++) { if( strRet.length > 1 )
strRet += “,”;
strRet += ExternalInterface.SerializeJsonToStr(objectJson[i]);
}
strRet += “]”;
return strRet;
}
if( typeof(objectJson) == typeof({}) )
{
var strRet = “{“;
for( var p in objectJson )
{
if( strRet.length > 1 )
strRet += “,”;
strRet += p.toString() + ‘:’ + ExternalInterface.SerializeJsonToStr(objectJson[p]);
}
strRet += “}”;
return strRet;
}
}

CentOSにmemcachedを起動するスクリプトコード

#vim /etc/init.d/memcached

サンプルコード:
#!/bin/sh
#
# memcached: MemCached Daemon
#
# chkconfig: – 90 25
# description: MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
#[ ${NETWORKING} = “no” ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z “$DUNDARGS” ] && exit 0
MEMCACHED=”/usr/local/memcached/bin/memcached”
SERVER_IP=”172.17.200.98″
SERVER_PORT=”11211″
[ -f $MEMCACHED ] || exit 1
start()
{
echo -n $”Starting memcached: ”
daemon $MEMCACHED -u daemon -d -m 2048 -l $SERVER_IP -p $SERVER_PORT -P /tmp/memcached.pid
echo
}
stop()
{
echo -n $”Shutting down memcached: ”
killproc memcached
echo
}
# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo $”Usage: $0 {start|stop|restart}”
exit 1
esac
exit 0

Struts開発でクライアントIPアドレスを取得する

javaコード:

public String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader(“x-forwarded-for”);
if(ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {
ip = request.getHeader(“Proxy-Client-IP”);
}
if(ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {
ip = request.getHeader(“WL-Proxy-Client-IP”);
}
if(ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}

Pythonでテキストファイルを読む方法まとめ

1.readline()
file = open(“startnews24python.txt”)
while 1:
line = file.readline()
if not line:
break
pass # do something
file.close()
2.fileinput
import fileinput
for line in fileinput.input(“startnews24python.txt”):
pass

3.readlines()
file = open(“startnews24python.txt”)
while 1:
lines = file.readlines(100000)
if not lines:
break
for line in lines:
pass # do something
file.close()

4.イテレータ
file = open(“startnews24python.txt”)
for line in file:
pass # do something
file.close()

Linuxでredis起動、停止コマンドメモ

1.apt-get またはyum installでredisをインストールする場合
/etc/init.d/redis-server stop
/etc/init.d/redis-server start
/etc/init.d/redis-server restart

2.ソースでredisをインストールする場合、redisクライアント側redis-cliのshutdownコマンドを操作する
停止
redis-cli -h 127.0.0.1 -p 6379 shutdown
起動 redis-server /etc/redis.conf

3.強制終了
ps -ef | grep redis
kill -9 pid