Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP and javascript

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • 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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
adil qureshi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic