2010-03-29 4 views
1
function modification() 
{ 

    alert(document.getElementById("record").value); 
    var rec=document.getElementById("record").value; 
    <% 
    Connection connect = DriverManager.getConnection("jdbc:odbc:DSN","scott","tiger"); 
    Statement stm=connect.createStatement(); 
    String record=""; // I want value of "rec" here. 

    ResultSet rstmt=stm.executeQuery("select * from "+record); 
    %> 
} 
+2

http://xkcd.com/327/ – simon

+0

http://balusc.blogspot.com/2009/05/javajspjsf-and-javascript.html – BalusC

답변

5

예제의 JavaScript는 클라이언트 브라우저에서 실행됩니다. JSP 코드는 서버에서 실행 중입니다. 서버의 클라이언트 측 데이터에 액세스하려면 클라이언트에서 서버로 데이터를 보내야하며, 예제에서와 같이 인라인 데이터에 액세스 할 수는 없습니다. 이것은 일반적으로 양식을 제출하거나 Ajax request을 수행하여 수행됩니다. Prototype를 사용하여 예를 들어

, 당신의 modification 기능은 다음과 같습니다 jQuery를 사용

function modification() 
{ 
    var rec=document.getElementById("record").value; 
    new Ajax.Request("modifyRecord.jsp", { 
    parameters: {rec: rec}, 
    onFailure: showUpdateFailure 
    }); 
} 

function showUpdateFailure(response) { 
    /* ...code here to show that the update failed... */ 
} 

또는, 그것은 다음과 같습니다

function modification() 
{ 
    var rec=document.getElementById("record").value; 
    $.ajax({ 
    url: 'modifyRecord.jsp', 
    data: {rec: rec}, 
    error: showUpdateFailure 
    }); 
} 

function showUpdateFailure(xhr, errmsg) { 
    /* ...code here to show that the update failed... */ 
} 

어느 쪽이든을, 당신의 modifyRecord.jsp는 것 데이터베이스 조작을 수행하는 데 사용할 수있는 rec POST 매개 변수를 수신하십시오 (SQL injection attacks을 방어하는 데주의하십시오).

관련 문제