2016-05-03 49 views
1

我有一個網頁,並有3個圖像,如此現在當 單擊圖像要設置這樣刪除會話之前的設定值

<a href="voting.jsp"><img onclick="myfunction('image_one')" src="image_one.jpg"></a> 
<a href="voting.jsp"><img onclick="myfunction('image_two')" src="image_two.jpg"></a> 
<a href="voting.jsp"><img onclick="myfunction('image_three')" src="image_three.jpg"></a> 
<script> 
function myfunction(name) 
    if(name='image_one') 
     <% session.setAttribute("user","image_one") %> 
    else if(name='image_two') 
     <% session.setAttribute("user","image_two")%> 
    elseif(name='image_three') 
     <% session.setAttribute("user","image_three")%> 
</script> 

會話屬性現在的答案常是image_three當我使用會話.getAttribute在voting.jsp頁面 爲什麼

回答

0

您正在嘗試在javascript/JQuery函數中使用服務器端代碼(scriptlet)。 Scriptlet代碼在服務器端執行,Web-Container(Tomcat服務器)發送乾淨的HTML頁面作爲響應。

所以基本上你在這裏做的都是錯誤的東西。您不能在JavaScript函數中設置會話。

您可以在此流程中工作: 1.設置任意變量的值,相應地選擇圖像。

2.通過提交頁面並按照相應的值設置會話,將此值發送給servlet。

Setting session variable using javascript。檢查這個。

0

將值設置爲會話刪除會話值之前。

<% session.removeAttribute("user"); 
    session.setAttribute("user","image_one"); %>