컴퓨터활용/자바

jsp 에서 unix 명령 실행하기

멜번초이 2008. 8. 1. 15:10
반응형

jsp에서 unix 의 command 를 직접 실행시키는 방법을 찾고 있다가 다음과 같은 글을 발견했다.
OS에서 명령을 실행할 수 있어야 하므로 웹유저로 실행권한이 있어야 합니다.

<%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import="java.io.*" %>

<%
Process process = null;
BufferedReader in = null;
BufferedReader err = null;

try {
    String command = "sh /home/test.sh";
    process = Runtime.getRuntime().exec(command);
 
   in =  new BufferedReader (new InputStreamReader(process.getInputStream()));
  while ((s = in.readLine ())!= null) {
       out.println(s+"<br>");
  }
  err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  while (err.ready()) {
        out.println(err.readLine()+"<br>");
  }
} catch (Exception e) {
    out.println("Error : "+e);
   System.out.println(new java.util.Date()+" process.jsp "+e);
} finally {
    if (in != null) try { in.close(); }  catch (Exception sube) {}
   if (err != null) try { err.close(); }  catch (Exception sube) {}
}
%>

또다른 의견으로는  server.jsp  파일에서  Runtime.exec() 메소드를 사용하여 실행할 수 있다는 자료가 있군요.

<%
Runtime.exec("경로/실행파일명");
%>

반대로 서버쪽이 아닌 client 쪽에서 html 을 통해  직접 어떤 exe 파일을 실행시키고자 한다면 다음과 같이 할 수 있다. 그러나 이 경우 exe 파일의 classid 를 알아야만 한다는 것이다. exe 파일이 아니더라도  ocx 타입 activeX 파일도 실행시킬 수 있다.  아래와 같이 html 을 만들면 화면이 열리자 마자 바로 실행되어 버릴 것이다.

<body>
<OBJECT NAME="X"  CLASSID="CLSID:1111111-1111-1111-1111-111111111111"
              CODEBASE="C:/WINDOWS/system32/aaa.exe">
</OBJECT>
</body>

만일 자바 애플릿을 잘 짠다면 애플릿을 만들어 html 속에 숨겨 놓아도 어떤 파일을 실행시킬 수 있을 것이다.


 

반응형