리소스와 클래스(Resource/Class) 의 실제 위치 찾기 JSP JAVA

실제 Class 의 위치를 검색하는 내용으로 JSP 로 사용하면 유용함..





resource.jsp 


<
%@pagecontentType="text/html;charset=euc-kr" %>
<html>
    <head><title>JSP Page</title></head>
    <body>
    <%
    request.setCharacterEncoding("euc-kr");

    String _resource = request.getParameter("resource");
    String _type = request.getParameter("type");
    %>
    <form action="resource.jsp" method="get">
        리소스 풀 네임 :
            <input name="resource" type="text" size="40" value="<%= _resource != null ? _resource : "" %>"><br>
        <input type="radio" name="type" value="class" checked="true">클래스
        <input type="radio" name="type" value="file">파일<br>
        클래스는 "java.lang.String" 과 같이 쓰고, 파일은 "/resources/ApplicationMessages.properties"와 같은
        형식으로 기입한다.<br>
        <input type="submit">
    </form>
   
    <%
        String path = null;
       
        if (_resource != null) {
            ResourceLocation rl = new ResourceLocation();

            if ("class".equals(_type)) {
                path = rl.getResourceURL(_resource, true);
            } else {
                path = rl.getResourceURL(_resource, false);
            }
    %>
    * 요청 리소스 : <%= _resource %><br>
    * 요청 타입 : <%= _type %><br>
    * 실제 리소스 경로 : <%= path %>
    <%
        }
    %>
    </body>
</html>

<%!
--*
 * <p>
 * Java Class 혹은 리소스(클래스패스 내에 있는 파일)의 위치를 정확히 알려준다.
 * </p>
 *
 * @author Son KwonNam(
kwon37xi@yahoo.co.kr)
 --
private static class ResourceLocation {

    --*
     * 패키지명을 포함한 클래스 풀 네임을 리소스 경로(/package/classname.class)로 바꿔준다.
     *
     * @param classFullName
     *            클래스 풀 네임(package.classname)
     * @return 리소스 경로 (/package/classname.class)
     --
    public String classFullNameToResourcePath(String classFullName) {
        String resourcePath = classFullName.replace('.', '/').trim();
        resourcePath = "/" + resourcePath + ".class";
        return resourcePath;
    }

    --*
     * 클래스 풀 네임을 받아서 실제 파일이 위치한 URL을 리턴한다.
     *
     * @param classFullName
     *            클래스의 풀 네임
     * @return 클래스 파일의 URL
     --
    public String getResourceURL(String classFullName) {
        return getResourceURL(classFullName, true);
    }

    --*
     * 클래스 혹은 리소스의 이름을 받아서 실제 파일이 위치한 URL을 리턴한다.
     *
     * @param resource
     *            리소스 이름
     * @param isClass
     *            클래스인지 여부
     * @return 리소스의 URL
     --
    public String getResourceURL(String resource, boolean isClass) {
        String refinedResource = null;

        // "/dir1/dir2/resource.ext" 형태로 바꾼다.
        if (isClass) {
            refinedResource = classFullNameToResourcePath(resource);
        } else if (!resource.startsWith("/")) {
            refinedResource = "/" + resource.trim();
        } else {
            refinedResource = resource.trim();
        }

        System.out.println("검색할 리소스 : " + refinedResource);

        java.net.URL resourceUrl = ResourceLocation.class
                .getResource(refinedResource);

        if (resourceUrl == null) {
            return null;
        }

        return resourceUrl.getFile();
    }
}
%>




출처 :
http://kwon37xi.egloos.com/1443423


HTTP connection timeout, read timeout (JDK1.4 vs JDK1.5) JAVA

Java 에서 HttpURLConnection 을 얻는 일반적 코드는 아래와 같다.

URL url = new URL("http://www.google.com/");  
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 

여기에서는, 
read timeout, connection timeout 이 끼어들 여지가 없어 보인다.
이를 위해, 아래의 2가지 시스템 프로퍼티를 설정함으로써 timeout 을 제어할 수 있었다.

System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");

Java 1.5 이전 버전에서는 오직 위의 2개 프로퍼티 설정에 의해 네트워크 속성의 타임아웃을 제어할 수 있다. 그러나, Java 1.5 에서는 아래와 같이 2개의 메소드를 제공한다.

HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
conn.setConnectTimeout(30000);  
conn.setReadTimeout(30000); 

전체적인 샘플 코드는 아래와 같다.
try 
{
  URL url = new URL("http://www.google.co.kr/");  
  HttpURLConnection con = (HttpURLConnection)url.openConnection();  
  con.setRequestMethod("POST");  
  
  // for jdk 1.4
  System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
  System.setProperty("sun.net.client.defaultReadTimeout", "30000");
  
  // for jdk 1.5
  con.setConnectTimeout(5000);
  con.setReadTimeout(5000);

    
  con.setDoOutput(true);  
  PrintWriter out = new PrintWriter(con.getOutputStream());  
  
  // out 으로 데이타를 쓰고 입력 스트림을 얻어 응답을 읽고 처리...
  ........
  
 
catch(Exception e)  
 
  e.printStackTrace();  
 


출처 :
http://charmpa.egloos.com/2931915

HashMap loop ( HashMap 루프문 ) JAVA


                                                                                                               

                                                                                                              
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {            
    Map.Entry entry = (Map.Entry) iter.next();                                        
    String key = (String)entry.getKey();                                                  
    String value = (String)entry.getValue();                                            
}                                                                                                            
                                                                                                              

1 2 3 4 5 6 7 8 9 10 다음



방문자 위치보기

[위자드팩토리] 예쁜 아기 키우기 - W위젯

[위자드팩토리] 온챗채팅