顯示具有 Java 標籤的文章。 顯示所有文章
顯示具有 Java 標籤的文章。 顯示所有文章

星期二, 10月 20, 2015

[Java] log4j 的設定檔配置筆記

先前有筆記一下如何設定log4j,這篇只要蒐集一些有關log4j.xml的一些工作手寫:D


  • log4j.xml 的優先權會大於 log4j.properties
  • log4j.jar不要放在每個application中
  • Web專案的話log4j.xml 的檔案路徑請放在 WEB-INF/classes/之下
  • Java專案的話可以建一個resoucrce資料夾,並把log4j.xml放在之下
  • 或著自行指定路徑,再利用DOMConfigurator.configure去讀取ex: DOMConfigurator.configure(log4jConfigurationFilename);

參考
http://wiki.apache.org/logging-log4j/Log4jConfigurationHelp

星期日, 8月 09, 2015

[Java] Granule Asset 函式庫測試

記錄一下使用Granule來編譯js/css檔的過程,雖然專案有點舊了,但試用起來效果還不錯。
主要是看上有JSP Tag很好整合。

功能如下

  1. Combine and compresses JS and CSS using different methods: on fly or in build process, CSS and JS fast compression or more sophisticated Google Closure compression, or just simple file combining.
  2. No-lock in solution. The tag just put around existing scripts. The tag can be turn on/off on the different levels: page and application.
  3. Debug and production modes.
  4. Calculate dependencies using Closure Library package/namespace system.
  5. Can automatically choose optimization methods.
  6. Multiple combinations of JS/CSS even with different compression methods on one page.
  7. Support JSP includes.
  8. Several types of cache, memory and file.
  9. Automatically regenerates the bundle if you modify an included file.
  10. Proxy-friendly GZip support.
  11. Rewrites relative URLs in your CSS files.
  12. JSP, JSF, Grails integration.
  13. Multiple loggers support (SLF4J, Log4J, Apache Logger)
  14. Can be setup to preserve license headers of JS libraries.
  15. JDK1.5 and higher even for Google Closure Compiler.

安裝

星期五, 8月 07, 2015

[Java] Granule 解決tomcat8 檔案路徑回傳null問題

https://github.com/JonathanWalsh/Granule

最近找了這個工具來幫js/css瘦身,在tomcat 8下轉換assets file路徑會在getRealPath下產生Exception。

The parameter for getRealPath() is a 'virtual path' which - unfortunately - is a concept used in the Java docs but not actually defined anywhere. It is assumed to be a path to a resource in your web application and the separator in that case is always '/' regardless of platform.

解決方法要在html頁面引用javascript file時,要多加一個/





星期六, 5月 09, 2015

[Java] Java專案的JS/CSS minify

針對Java專案js/css最小化相關open souces資源記錄

pack-tag

A JSP Taglib for delivering minified, combined and gzip-compressed resources (JavaScript and CSS).

https://github.com/galan/packtag

Jawr

https://github.com/ic3fox/jawr


Granule

Java JS/CSS optimization solution (tag and ant,servlet). Whitespace remover and Google Closure compilator integration
https://github.com/JonathanWalsh/Granule


wro4j

Free and Open Source Java project which brings together almost all the modern web tools: JsHint, CssLint, JsMin, Google Closure compressor, YUI Compressor, UglifyJs, Dojo Shrinksafe, Css Variables Support, JSON Compression, Less, Sass, CoffeeScript and much more. In the same time, the aim is to keep it as simple as possible and as extensible as

https://github.com/wro4j/wro4j



星期四, 3月 12, 2015

[Java] JPA 使用IN查詢

由於不習慣用jpa來操作db,第一次要使用in查詢式就踩雷了。
成功的方法如下:

  String cmd = "SELECT d FROM Device d  WHERE d.agentId IN :agentId";
            Query query  = entityManager.createQuery(cmd);
            query.setParameter("agentId", dealAgentIds);

備註: 

  • 不要把 :agentId多包一個括號,例如(:agentId),會炸給你看的。 
  • d.agentId是你產生結構裡面的欄位命名,不要用原本database的欄位。

星期三, 1月 28, 2015

[Android] Android Studio 安裝記錄

最近要開始把玩Andorid,由於新手入門大家都建議不用完Eclipse ADTXD,
直接換Android Studio開發可能對長遠比較好,加上AS的功能與性能似乎幹爆Eclipse。
以下是安裝過程中炸掉的處理方法

安裝步驟

Step1: 遇到安裝JDK前會爆炸的話,請先下載這個補丁XD
http://support.apple.com/kb/DL1572?viewlocale=zh_TW&locale=en_US
Step2: 安裝JDK
Step3: 指定AS要用的JDK路徑,之後就一直下一步Next

網路上額外找的相關JDK的安裝路徑
For different types of JDKs or installations, you will have different paths.
On Mac OS X Mavericks, the path I found as following:
1) Built-in JRE default:
 /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
2) JDKs downloaded from Apple:
 /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/
3) JDKs downloaded from Oracle:
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home

Step4: 開始享用

參考

http://blog.ccc99.tw/2014/10/os-x-1010-java.html

星期二, 1月 20, 2015

[Java] Unable to load class for JSP 炸掉筆記

20150204更新:
最後發現有重覆的jar甘擾造成慘劇!!

  • jasper-compiler-5.5.23.jar
  • jasper-runtime-5.5.23.jar
  • jsp-2.1-6.1.14.jar
  • jsp-api-2.1-6.1.14.jar



好死不死遇到Unable to load class for JSP ,
手記這個bug的處理情況, 問了google大神也一堆人遇到
但沒統一的解法,有的人是用到的jar有衝突還是缺之類的,還是classpath跑掉blabla
目前的狀況整個IDE新舊專案的JSP都無法編譯。
索性就用重灌ide的方法,結果執行的時候也爆了。
原來一灌的時候,忘了設Tomcat的VM Options(會導致libs無法載入太多而炸掉)
接著就能跑了....


炸掉的error



星期一, 1月 19, 2015

[NetBeans] 設定Utf-8編碼

預設的netbeans好像沒有gui設定utf-8編碼開啟檔案,可以透過修改設定檔的方式來解決

步驟:


1. 打開設定檔:
C:\Program Files (x86)\NetBeans 7.2.1\etc\netbeans.conf

2. 找到netbeans_default_options參數

3. 加入-J-Dfile.encoding=UTF-8


星期三, 7月 16, 2014

[Java] 簡單的User authorization機制

常用到的使用者是否有正確的權限存取當前頁面的機制,Java可以透過Filter來達到。
可依個人需求再強化嚕。

This can be handled in a Filter and there are great explanation and example in StackOverflow Servlet-Filter wiki.
Adapting the code there for your problem (note the addition and usage of the needsAuthenticationmethod):
@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig config)
        throws ServletException {
        // If you have any  in web.xml, then you could get them
        // here by config.getInitParameter("name") and assign it as field.
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession(false);

        String requestPath = httpServletRequest.getRequestURI();

        if (needsAuthentication(requestPath) ||
            session == null ||
            session.getAttribute("user") == null) { // change "user" for the session attribute you have defined

            response.sendRedirect(request.getContextPath() + "/login"); // No logged-in user found, so redirect to login page.
        } else {
            chain.doFilter(req, res); // Logged-in user found, so just continue request.
        }
    }

    @Override
    public void destroy() {
        // If you have assigned any expensive resources as field of
        // this Filter class, then you could clean/close them here.
    }

    //basic validation of pages that do not require authentication
    private boolean needsAuthentication(String url) {
        String[] validNonAuthenticationUrls =
            { "Login.jsp", "Register.jsp" };
        for(String validUrl : validNonAuthenticationUrls) {
            if (url.endsWith(validUrl)) {
                return false;
            }
        }
        return true;
    }
}
I would recommend to move all the pages that require authentication inside a folder like app and then change the web filter to
@WebFilter("/app/*")
In this way, you can remove the needsAuthentication method from the filter.

星期一, 5月 26, 2014

[Java] 快快樂樂學會log4j

最近弄log4j的同事離職,趁記憶還在的時候,
自已親身再run一遍,本文大多取自Google來的各文章的筆記。

log4j使用版本:1.2.x

星期五, 4月 25, 2014

[Java] 解析IE的版本

不透過第三方套件取得瀏覽器的主要版本!! ps: ie11的user-agent需變換判斷方式msie => trident
public class BrowserVersionDetect {
 
  public static void main(String[] args) {
   
  String userBrowser = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; MATP)";
  userBrowser = userBrowser.toLowerCase();
   
  String browserVersion=userBrowser.substring(userBrowser.indexOf("msie")).split(";")[0];
        String majorNumber 
         = browserVersion.split(" ")[1];

  System.out.println("browserVersion:" + browserVersion);
  System.out.println("majorNumber:" + majorNumber);
  
  }
  
}

星期五, 3月 21, 2014

Log rotation 機制

查資料發現的tool,如果你的log檔愈來愈大的時候,就需求實作log rotation的機制。
cronolog就是幫我們解決這件事。 詳細資料請參考官網: http://cronolog.org/

***
參考:
使用 cronolog 作 Apache log 的分割處理

星期三, 3月 19, 2014

[TOMCAT] 利用Eclipse Memory Analyzer 來分析heap dump file

記錄一下MAT的操作心得

如何取得java heap dump

請再catalina.sh啟動Java heap dump的設定
JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/TOMCAT/logs/heap-$(date +%Y-%m%d-%H%M).dump"


-XX:+HeapDumpOnOutOfMemoryError 打開heap dump
-XX:HeapDumpPath heap dump要產生的路徑



星期一, 12月 02, 2013

[Java] 如何將上傳streaming透過Http Client再上傳到另外一個Server

目前有一個需求是要將上傳api拿到的Stream再透過REST API上傳至另一個儲存空間。
採用的架構是透過Java Jersey + Apache HttpClient 4.3

測試範例如下:

其他你感興趣的文章

Related Posts with Thumbnails