Tag: java

[JAVA] How to send HTTP GET with params

Sorry, this entry is only available in 中文.

Tags : ,

[QA] how to remove HTML from a java string

Sorry, this entry is only available in 中文.

Tags : , ,

[QA] singleton pattern in java

public Foo {
   private static final Foo INSTANCE = new Foo();
 
   public static Foo instance() {
      return INSTANCE;
   }
}
public enum Foo {
   INSTANCE;
}
Tags :

[QA] How to check java class built version

$ file ~/bin/classes/MyClass.class
 /home/abc/bin/classes/MyClass.class: compiled Java class data, version 50.0
  • 46 = Java 1.2
  • 47 = Java 1.3
  • 48 = Java 1.4
  • 49 = Java 5
  • 50 = Java 6
  • 51 = Java 7
Tags :

[JAVA] Runtime.getRuntime().addShutdownHook

Before jvm finishs, it will run the thread added to Runtime.getRuntime().addShutdownHook

good for watch dog

public void attachWatchDog() {
   Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
         System.out.println("your last chance before program terminated");
      }
    });
}
Tags :

[QA] How to get clipboard strings in JAVA SWT

Clipboard clipboard = new Clipboard(Display.getDefault());
// copy to clipboard
clipboard.setContents(new String[]{"My Text in Clipboard"}, new Transfer[]{TextTransfer.getInstance()});
// paste from clipboard
String text = clipboard.getContents(TextTransfer.getInstance());
Tags : , ,

[QA] How to use regular expression to find year, month and day of date

String aDate = "date: 04-16-2013";
Pattern datePattern = Pattern.compile("date: (\\d{2})-(\\d{2})-(\\d{4})");
Matcher dateMatcher = datePattern.matcher(aDate);
if (dateMatcher.find()) {
   System.out.println("Month is: " + dateMatcher.group(1));
   System.out.println("Day is: " + dateMatcher.group(2));
   System.out.println("Year is: " + dateMatcher.group(3));
}
Tags : ,

[問題] servlet 如何獲得 request URL

假設 Request Servlet URL 如下:

http://hostname.com/mywebapp/servlet/MyServlet/a/b;c=123?d=456

獲得 RequestURL 完整路徑:

/**
*
* @param req
* @return http://hostname.com/mywebapp/servlet/MyServlet/a/b;c=123?d=456
*/
public static String getRequestURL(HttpServletRequest req) {
   String reqUrl = req.getRequestURL().toString();
   String queryString = req.getQueryString(); // d=456
   if (queryString != null) {
      reqUrl += "?"+queryString;
   }
   return reqUrl;
}

獲得 Request URI:

/**
*
* @param req
* @return /mywebapp/servlet/MyServlet/a/b;c=123?d=456
*/
public static String getRequestURI(HttpServletRequest req) {
   String reqUri = req.getRequestURI().toString();
   String queryString = req.getQueryString(); // d=456
   if (queryString != null) {
      reqUri += "?"+queryString;
   }
   return reqUri;
}

獲得 Request Root URL:

/**
*
* @param req
* @return http://hostname.com/mywebapp
*/
public static String getRootURL(HttpServletRequest req) {
   String scheme = req.getScheme(); // http
   String serverName = req.getServerName(); // hostname.com
   int serverPort = req.getServerPort(); // 80
   String contextPath = req.getContextPath(); // /mywebapp
// String pathInfo = req.getPathInfo(); // /a/b;c=123
// String queryString = req.getQueryString(); // d=456
   String url = scheme+"://"+serverName+":"+serverPort+contextPath;
   return url;
}
Tags : ,

[問題] 如何使用 regular expression 判斷 alphanumeric and underscores 字串

String regex = "^[a-zA-Z0-9_]*$";
String words = "some words ConTain 123_";
 
if (words.matches(regex)) {
// only contains alphanumeric and underscores
}
Tags : ,