Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSP and the fly likes Retrive variable from JavaScript to JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Retrive variable from JavaScript to JSP page" Watch "Retrive variable from JavaScript to JSP page" New topic
Author

Retrive variable from JavaScript to JSP page

Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
Hello,

I need to pass the content of a variable from a javascript function to a jsp page. The content of the variable is huge, so I can't send it via URL.

Thanks.


Poor is the man whose pleasures depend on the permission of another.
rahul khanna
Ranch Hand

Joined: Sep 14, 2005
Posts: 48
The simplest way to do is to have a hidden field, textbox, on the page and assign the text value of it in jscript.
You can then use tht value in jsp or even in a servlet on form submission.

Hope this helps.
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
Okay I did it, but now I don't know how to get the content from the textarea for example. I don't have any submit button.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Then how do you plan to submit the form?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
Bear Bibeault wrote:Then how do you plan to submit the form?


Well there's no submit form, I'm making some operations in a javascript function by clicking on a link. From there I need to parse the result to a jsp page, thats why I needed a textarea.
I hope you understand what I mean
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Are you talking about getting this data to the "next" JSP, or to the JSP that resulted in the current page being displayed?

If the former, then a submit of some type has to happen in order to transition to the next JSP.

If the latter, then you need to find out a bit more about how JSP works. Perhaps this article will help.
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
I agree, i'm new to JSP. I was reading that article just now.

I have only one JSP file, all the operations are made via JavaScript.

This is the core of my JSP page:



Before that core I have put an invisible textarea which will have the result from some calculations. I need to check with JSP if the textarea is empty, if not to write/send that content somewhere. Hope this clears a bit things up.
Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Read the article. What you are trying to do is impossible.

The JSP code executes on the server in order to a format an HTML page to send to the client. By the time the HTML is interpreted and the JavaScript executes, the JSP has long since done its job.

Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
I just finished reading the JSP article and now I understand my problem. I guess I'm stuck, I needed to write that result on my server, since JavaScript doesn't allow it. Any sugestions?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

To get data from the client to the server requires an HTTP request. Whether that's a form submission, a link traversal or an Ajax request depends upon the nature of the page and what you want to happen.

If you want it to happen quietly, behind the scenes with no disruption to the page and under JavaScript control, that's what Ajax is for.

Or you might just want to back up a bit and re-evaluate the whole design of the page. Are you doing things the best way, or just trying to hodge-podge? You haven't given us enough context to evaluate.
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
Thanks for the help, I've redesigned my application and used serlvets + JSP, problem solved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrive variable from JavaScript to JSP page