Monthly Archives: 12月 2015

「jQuery入門」$(“input”).trigger()のサンプル

JSコード
$(document).ready(function(){
$(“input”).select(function(){
$(“input”).after(“選択された”);
});
var ff = jQuery.Event(“select”);
$(“button”).click(function(){
$(“input”).trigger(ff);
});
});
</script>
</head>
<body>
<input type=”text” name=”FirstName” value=”Hello World” />
<br />
<button>inut select event</button>

JavaでクライアントIPアドレスを取得する方法

サンプルコード:
private static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader(“X-Real-IP”);
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;
}
}

Java入門 HTMLタグの要素を取り出す正規表現式

1. <a href=”…”>~</a>のように属性を持つタグの要素を取り出す
Javaコード:
String patternStrs=”(<//s*a//s+(?:[^//s>]//s*){0,})href//s*=//s*(/”|’|)([^//2//s>]*)//2((?://s*[^//s>]){0,}//s*>)”;
PatternCompiler complier = new Perl5Compiler();
PatternMatcher matcher = new Perl5Matcher();
Pattern patternForLink = complier.compile(patternStrs, Perl5Compiler.
CASE_INSENSITIVE_MASK);
PatternMatcherInput input = new PatternMatcherInput(htmlContent);
while (matcher.contains(input, patternForLink)) {
MatchResult match = matcher.getMatch();
}

2.<img src=”…”>のように属性を持つタグの要素を取り出す
(</s*img/s+(?:[^/s>]/s*){0,})src/s*=/s*(“|’|)([^/2/s>]*)/2((?:/s*[^/s>]){0,}/s*>)

Google Feedフィードを読み込むサンプル

コード下記

var pastDay = 15;//何日数前までnewマークをつけるか指定
var now = (new Date()).getTime();//現在の時間
var pastTime = pastDay * 24 * 60 * 60 * 1000;//newマークをつける期間の時間

function initialize() {
var feed = new google.feeds.Feed(“http://xxx.com/?feed=rss2&cat=3”);
feed.setNumEntries(5);
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById(“newssfeedtable”);
var html = “”;
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var dd = new Date(entry.publishedDate); // now
var yearNum = dd.getYear();
if (yearNum < 2000) yearNum += 1900;
var m = dd.getMonth() + 1;
if (m < 10) {m = “0” + m;}
var d = dd.getDate();
if (d < 10) {d = “0” + d;}
var date = yearNum + “年” + m + “月” + d + “日”;
html += “<li>” + date;

if(now >= dd.getTime() && now <= (dd.getTime() + pastTime)){
//newマークの追加
html += “<img src=’../img/icon-text-new.png’ alt=’新着’>”;
}
//console.log(“now:” + now + ” dd.getTime:” + dd.getTime() + ” pastTime:” + pastTime);

html += “<br><a href='” + entry.link + “‘ target=’_blank’>” + entry.title + “</a></li>”;

container.innerHTML = html;
}
}
});
}
google.setOnLoadCallback(initialize);

StrutsでDataSourceを利用する方法

1.Actionのexecute
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
//ユーザとパスワードを取得
LoginForm loginform = (LoginForm)form;
String username = loginform.getUsername();
String password = loginform.getPassword();
System.out.println(“————–“+username+”————-“);
System.out.println(“————–“+password+”————-“);
DataSource ds = this.getDataSource(request, “dataSource”);
boolean result = false;
try {
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(“select * from startnews24_user where username='”+username+”‘ and password='”+password+”‘”);
if(rs.next()) {
result = true;
} else {
result = false;
}
rs.close();
stmt.close();
conn.close();
if(result) {
return mapping.findForward(“welcome”);
} else {
return mapping.getInputForward();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

2.struts-config.xml
<data-sources>
<data-source key=”dataSource” type=”org.apache.commons.dbcp.BasicDataSource”>
<set-property property=”username” value=”startnews24_user” />
<set-property property=”password” value=”startnews24_pwd” />
<set-property property=”driverClassName” value=”com.mysql.jdbc.Driver” />
<set-property property=”url” value=”jdbc:mysql://localhost:3306/startnews24-db” />
</data-source>
</data-sources>

3.jarファイル
commons-dbcp.jar,commons-pool.jar,commons-collections.jar

jQueryでbackground-imageを設定するサンプル

コード下記:
$(document).ready(function () {
$(“#Overview”).css(“background-image”,”url(./img/ChangFaStartnews24.png)”);
$(“#Overview div”).show();
$(“#tab li”).hover(function () {
$(“#tab li”).css(“background-image”,”url(./img/btnGray.png)”);
$(“#tab ul div”).hide();
$(this).css(“background-image”,”url(./img/ChangFaStartnews24.png)”);
$(this).children(‘div’).show();
});
})

JavaScriptで文字列の前後の空白を取り除く方法

//左の空白;
function ltrim(s){
return s.replace(/^(\s*|*)/,””);
}
//右の空白;
function rtrim(s){
return s.replace(/(\s*|*)$/,””);
}
//左右の空白;
function trim(s){
return ltrim(rtrim(s));
}

JavaScriptで数値であるかのチェツク方法

コード:

function checkInt(s){
var flag=true;
var Numstr=”0123456789″;
for(var i=0;i<s.length;i++){
var chr=s.charAt(i);
if(NumStr.indexOf(chr,0)==-1)
{
flag=false;
break;
}
}
return flag;
}

Javaで16(32)ビット文字列のmd5値を取得するサンプルコード

Javaコード下記:

private synchronized String md5s(String plainText)
{
try
{
MessageDigest md=MessageDigest.getInstance(“MD5”);
md.update(plainText.getBytes());
byte b[]=md.digest();
int i;

StringBuffer buf=new StringBuffer();
for(int offset=0;offset < b.length;offset++){
i=b[offset];
if(i < 0)
i+=256;
if(i < 16)
buf.append(“0”);
buf.append(Integer.toHexString(i));
}
str=buf.toString();
System.out.println(“32ビットの暗号結果:”+buf.toString());
System.out.println(“16bitの暗号結果:”+buf.toString().substring(8,24));

return str;
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
return null;
}
}

2 / 212