• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JSP and javascript

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible that, in a JSP page, I include a javascript section like


*****************
<% import %>

<!-- some JSP and html portion -->

<input type="button" onClick="verify()"/>

<script>
function verify() {
var name = document.getElementbyID("name");
var street = ...

<% Contact contact = new Contact(name, street);
session.setAttribute("contact", contact);
%>

window.open("/newAction");
}


</script>

******************

What I want to do is --- I want to create a java object inside the javascript, and pass it to a new action page. On the new action page, it retrieves the object from session, then use this object's data to access database and finally get the results and display it. Basically I need to use the client side information (what user selects) and I want to somehow pass it to a struts action or servlet or JSP by passing the data in a session. But I don't know if this idea is valid -- can I create java object inside javascript ? and can I embed java code inside javascript ? If I can't how can I save a client side DOM info into session ? Note -- I don't want to submit the whole thing to server by using a submit button. I have separate buttons to handle that, this is just a verify button in the middle stage.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't pass/access Java object in JS. What you can do is either pass the data using query string OR POST them using FORM.
 
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javascript code gets executed at the client(browser).

And java code gets executed at Server. Server knows about session, request, response objects.

Hence you cannot do this.
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As already pointed out, impossible. Please read this article to understand how JSP works and why that is not possible.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Store the data in some variables send this data to server using AJAX based request....
this wont submit the form either ....
..... here you can create your java object .... then store it in some session variable and then you can perform you data base logic .
 
Evildoers! Eat my justice! And this tiny ad's justice too!
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic