1

I have a page and there are 3 images in that so now when click an image want to set session attribute like this

<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>

now the answer is alway image_three when i use session.getAttribute in the voting.jsp page why

Sandun Chathuranga
  • 2,242
  • 2
  • 13
  • 27

2 Answers2

0

You are trying to use server side code(scriptlet) in javascript/JQuery function. Scriptlet code executes at server side and Web-Container(Tomcat Server) sends clean HTML page as a response.

So basically you are doing all wrong stuff here. You can't set session in JavaScript function.

You can work in this flow : 1.Set value of any variable accordingly image is selected.

2.Send this value to servlet by submitting page and set session by accordingly value you get.

Setting session variable using javascript. Check this.

Community
  • 1
  • 1
Darshit
  • 361
  • 2
  • 13
0

Before set value to session remove session value.

<%  session.removeAttribute("user");
    session.setAttribute("user","image_one"); %>
Sandun Chathuranga
  • 2,242
  • 2
  • 13
  • 27