aspose file tools*
The moose likes JSP and the fly likes populating a javabean with the return value of javascript function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "populating a javabean with the return value of javascript function" Watch "populating a javabean with the return value of javascript function" New topic
Author

populating a javabean with the return value of javascript function

Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 85
Hi,
I'm trying to modify some text in a textarea wtih javascript, and submit that modified value to a javabean via a jsp page.

Here is the form in my jsp page:



here's the jsp file it's going to:




somewhere along the way I'm screwing up the placement of the onsubmit att....also, there may be good reason not to do this with javascript, but it's bugging me that I can't.
BTW...I didn't post the javascript function (tagTextAsXML) but it does work.

any ideas?
thanks!
bp
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

In all that code I can't see the forest for the trees. It's be best to point out the pieces that you are referring to.

Edmund Castermund wrote:but it's bugging me that I can't.

You will continue to be bugged because it simply isn't possible. JavaScript executes on the client after the HTML page created by the JSP is created on the server.

Please read this article to understand the principles of JSP.

If you want to send data back to the server, you'll need to submit a new request.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

This sort of question has come up often enough that it now has a new JspFaq entry.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

In wading through the code, it looks like you might have created the second JSP just to set the property?

Firstly, a poor choice of technology. A JSP should be used for view creation, and nothing else.

Secondly, the bean generated by that page dies with the page. So what's the point?

Perhaps what you need to do is to describe what you are really trying to accomplish. This is perhaps a classic "X Y" problem. You are asking how to do Y because you've decided that it's what you need to do in order to accomplish X; but it turns out that it's not, and you really should be asking how to do X in the first place.

And lastly, the JSP actions setProperty and getProperty are JSP1 dinosaurs that were obsoleted in 2002 with the introduction of JSP2. You should be using the JSTL and EL in modern JSP.
Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 85
thanks for the replies, and apologies for the code excerpt. I had done some googling and had gotten the idea that this may not work, but thanks for confirming.

What I'm trying to do, is to take all the values of the form and pass to a java bean. I had hoped to code the values as xml with javascript, but I think it would be better to have the java bean do the xml coding server side.

My thinking (apparently incorrect) re: the mulitple requets was that a) the user clicks the link to the form jsp page, and it loads. b) the user fills in all the values, and c) when the user clicks submit, the javascript would be invoked, thus sending the coded string to the server as a param.

But that's not how it works, I guess. Anyway, thanks for pointing out the need for jstl, I'll definitely read up on that.
bp
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Edmund Castermund wrote:What I'm trying to do, is to take all the values of the form and pass to a java bean.

Simple. Submit the form to a servlet, create a bean, set the properties of the bean from the submitted parameter. That's a ubiquitous scenario.

I had hoped to code the values as xml with javascript

Why?

but I think it would be better to have the java bean do the xml coding server side.

Yes. Or at least server-side code. I'm not sure a bean is the right choice (yet).

My thinking (apparently incorrect) re: the mulitple requets was that a) the user clicks the link to the form jsp page, and it loads. b) the user fills in all the values, and c) when the user clicks submit, the javascript would be invoked, thus sending the coded string to the server as a param.

Well, that is pretty much how it works. Except I don't see the need for intervening JavaScript, except maybe to validate the form. At least in this scenario.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: populating a javabean with the return value of javascript function