Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting session data with javascript

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP page :

In which i would create a session .


I have a button with onclick event

<input type="submit" value="Add" onclick=SetData()/>


 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it isn't.

Javascript is to Java like Carpet to Car. Only the first few characters match, but further they have completely nothing to do with each other.

Use a Servlet class.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your valuable answer.

<input type="submit" value="Add" onclick= <% session.setAttribute("fruits", fruit); %>

I am afraid to post the above code , but posted
 
Ankit Garg
Sheriff
Posts: 9520
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RaviNada your second code will also not work. If you want to set a session attribute on the click of a button, then either submit the page to a servlet or make an ajax request to a servlet which will set the session attribute...
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me what is the problem with the second code??
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java/JSP runs at the server machine and produces a HTML page.
The HTML page may contain other clientside languages like CSS/JS.
Once Java/JSP is finished producing the HTML page, it sends it to the client side.
Once arrived at the client side, the client starts to interpret the HTML/CSS/JS code.
Do a 'view source' in your webbrowser, you'll see that there is no means of Java/JSP code in there.

Learn the meaning of "server side" and "client side".
Go get yourself a decent JSP/Servlet book. This all is fairly trivial.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bauke that was a excellent explanation.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learn the meaning of "server side" and "client side".
Go get yourself a decent JSP/Servlet book. This all is fairly trivial.


Please stop this behaviour from you . It hurts people.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic