サーバ情報
log.warn("request.getContextPath():"+request.getContextPath()); log.warn("request.getServletPath():"+request.getServletPath()); log.warn("request.getPathInfo():"+request.getPathInfo()); log.warn("request.getPathTranslated():"+request.getPathTranslated()); log.warn("request.getRequestURL():"+request.getRequestURL()); log.warn("request.getRequestURI():"+request.getRequestURI()); log.warn("request.getHeader('Referer'):"+request.getHeader("Referer")); log.warn("request.getHeader('Content-Location'):"+request.getHeader("Content-Location")); log.warn("request.getHeader('Location'):"+request.getHeader("Location"));
jspからドキュメントルートを取得する
String ownUrl = request.getRequestURL().toString(); String docRoot = ownUrl.substring(0, ownUrl.indexOf("WEB-INF/"));
数字を数値に変換したい
int i = Integer.parsInt("1"); float f = Float.parsFloat("1.0"); double d = Double.parsDouble("1.0"); // 以下、コモンズは非推奨なので使用しないこと NumberUtils.stringToInt(String str); NumberUtils.stringToInt(String str, int defaultValue);
数値を数字にしたい
String val = String.valueOf(1); もしくは String val = Integer.toString(1);
Booleanの値を取得したい
Boolean trueValue = Boolean.TRUE; これは駄目 Boolean trueValue = new Boolean(true);
フォーマッティング
String s = null; s = String.format("%,d", 1000000000); // => 1,000,000,000 s = String.format("%.2f", 3.1434); // => 3.14 s = String.format("%tc", new Date()); // => 月 11 06 21:53:25 JST 2006 s = String.format("%tr", new Date()); // => 09:52:53 午後 Date today = new Date(); s = String.format("%tA, %tB, %td", today, today, today); // => 月曜日, 11月, 06
正規表現
$nでグループを置換する方法
public String test(String str) { if (str == null || str == ""){ return ""; } Pattern p1 = Pattern.compile("[\n\r\t]"); Pattern p2 = Pattern.compile("([() ])"); Matcher m1 = p1.matcher(str); String res = m1.replaceAll(" "); Matcher m2 = p2.matcher(res); StringBuffer sb = new StringBuffer(); while (m2.find()){ m2.appendReplacement(sb, "\\\\$1"); } m2.appendTail(sb); return sb.toString(); }