Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

setting session data with javascript

 
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()/>


 
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
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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.
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic