2016-07-21 19 views
0

我試圖使用提交按鈕將2個參數userIdfriendId,使用jsp傳入同一個servlet。我能夠獲得friendId,但是,我無法獲得userId使用onclick事件將2個參數提交到同一個servlet中

function anotherPageServlet(servletName) { 

    document.forms[0].method = "post"; 
    document.forms[0].action = servletName; 
    document.forms[0].submit(); 
} 

function anotherPageServlet1(servletName) { 

    document.forms[0].method = "post"; 
    document.forms[0].action = servletName; 
    document.forms[0].submit(); 
} 

<input style="color:#3333ff" 
     type="submit" 
     class="rsubmit" 
     value="<%= retrieving.getDisplayname()%> :" 
     onclick="anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute("userId") %>'), 
anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>')" 
> 
+0

有你有兩個相同的JS功能有特殊原因? –

+0

@Hubert Grzeskowiak沒有理由。我主要的動機是將2個參數userId和friendId一起傳遞給servlet。 – qcc

回答

0

你可能只需要逃避報價:

onclick="anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute(\"userId\") %>'), 
anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>')" 

我不習慣JSP,但JSF你寧願值分配到請求範圍的bean,然後調用一個函數與那個bean一起工​​作,而不是直接傳遞這些參數。

編輯:

定義的onclick第一功能,則調用僅簡單參考。

function servletFoo() { 
    anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute("userId") %>'); 
    anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>'); 
} 

然後在你的HTML:

<input ... onclick="servletFoo()" /> 
+0

對不起,有一些語法錯誤。有沒有其他方法可以將這些值同時傳遞給servlet? – qcc

+0

對於語法錯誤感到抱歉。在這裏無法測試。您可能需要在腳本塊中定義JS函數,並僅將新創建的函數傳遞給onclick。這樣你的嵌套字符串就少了一層。查看更新的答案 –

相關問題