Skip to content

Instantly share code, notes, and snippets.

package test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
// http://localhost:8080/JerseySample/sample/hello?name=aaa&age=100
@Path("/hello")
public class HelloResource {
@daichan4649
daichan4649 / TestServlet.java
Last active August 29, 2015 14:01
tomcat7 basic authentication
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// logon user name
final String userName = request.getUserPrincipal().getName();
PrintWriter out = response.getWriter();
out.println(String.format("doGet(%s)", userName));
out.flush();
out.close();
@daichan4649
daichan4649 / IpListServlet.java
Created May 29, 2014 05:25
アクセスログ保存/一覧表示 (jQuery, JSON, JavaEE6)
package test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@daichan4649
daichan4649 / PropertyUtil.java
Last active August 29, 2015 14:02
Servletでのプロパティファイル読み書き
public class PropertyUtil {
private static final String PROPERTY_FILE_TEST = "test.properties";
public static void setTestPropertyValue(ServletContext context, String key, String value) {
setPropertyValue(context, PROPERTY_FILE_TEST, key, value);
}
public static String getTestPropertyValue(ServletContext context, String key) {
return getPropertyValue(context, PROPERTY_FILE_TEST, key);
@daichan4649
daichan4649 / ZipUtil.java
Last active August 29, 2015 14:02
unzip (for Java)
public static void unzip(String zipFilePath, File dstDir) {
try {
final ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> enumZip = zipFile.entries();
while (enumZip.hasMoreElements()) {
final ZipEntry zipEntry = enumZip.nextElement();
// 出力先設定
File dstFile = new File(dstDir, zipEntry.getName());
if (dstFile.isDirectory()) {
@daichan4649
daichan4649 / Util.java
Last active August 29, 2015 14:02
クラスパス内ファイルのフルパス取得
// クラスパスルートからの相対パスを指定
private static final String relativePath = "/resources/aaa.txt";
private String getRealResourcePath(String relativePath) {
return getClass().getClassLoader().getResource(relativePath).getPath();
}
@daichan4649
daichan4649 / DebugUtil.java
Created December 9, 2014 02:23
JavaEE 環境でクラスパス配下のファイルを文字列として読み込む(JSON等)
// dir name(class pathをルートとする相対パス)
private static final String DIR_NAME = "json";
private static final String FILE_NAME = "test.json";
public static String createJsonText(Class<? extends HttpServlet> clazz) {
return createJsonText(clazz, DIR_NAME, FILE_NAME);
}
public static String createJsonText(Class<? extends HttpServlet> clazz, String dirName, String fileName) {
String dirPath = clazz.getClassLoader().getResource(dirName).getPath();
@daichan4649
daichan4649 / JsonUtil.java
Created December 9, 2014 04:42
gson sample (JSON -> model)
public interface JsonModel {
// model
}
public class JsonUtil {
@SuppressWarnings("unchecked")
public static <T> T fromJson(String jsonText, Class<? extends JsonModel> T) {
Gson gson = new Gson();
return (T) gson.fromJson(jsonText, T);
}
@daichan4649
daichan4649 / MainActivity.java
Created February 23, 2012 03:56
HandlerThread
package test.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
@daichan4649
daichan4649 / CustomImageView.java
Created July 10, 2012 11:41
ListFragment+LruCache
package test.fragment.list;
import test.fragment.list.ImageFactory.Type;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;