• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Servlets & JSP book - JSP Question

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, good day , everyone .. my question is how javascript variable assign/save into java variable ?

following are jsp scriptlet and javascript example :

jsp:



javascript :


how do i save javascript variable under jsp ? thank you very much ~

[ October 21, 2004: Message edited by: Alvin chew ]
[ October 21, 2004: Message edited by: Alvin chew ]
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have understood your question correctly, you want to assign a JavaScript variable into JSP variable, isnt it?

In this case, I dont think this action is possible because:
1. JSP variables are server-side, they are translated BEFORE the response is sent to the browser
2. JS variables are client-side, they WONT available until the browser received the response

Thus, when your JavaScript variable gets the value due to your action (mouse click, etc), the JSP variable has been gone already (being parsed and JSP is converted into HTML).

Nick
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you could generate javascript with JSP, so you could assing variables on the server side.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're thinking of doing it on the same JSP, the answer is no. But, if you're hoping to do it on other pages or servlets, you can.

Just use hidden field. You can, however, assign Java values to Javascript. BTW, your code <% Boolean flag = "false" %> is not correct. It could be <% boolean flag = false; %> or <% Boolean flag = new Boolean("false"); %>

HTH.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But you could generate javascript with JSP, so you could assing variables on the server side.

Yes, this works because JSP can generate JS in server-side, but then, you cannot generate JSP using JS in the client side.

Nick
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely right Nicholas
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, if you write an application that thinks you need to generate JSP via some JS variables, you may:
- either redesign the logic
- as other said, submit the variables and use it in next page
- submit the variables to the SAME page, and display it

Nick
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks all for giving advices ,



as for above mentioned advice, can someone please show me some sample on it ? i not clear on the idea how to submit variable and use for next page ?
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need a form and the variables will be submitted with GET or POST.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a form to submit the value to the server again, in case you wanna the *next page* or the *same page* to use the variable.

Next page means, since we have no way to set a JSP value by JS value, thus, if we submit the JS value back to the server, assign it to JSP value at the server, and then re-generate the page back to the browser, you can then get the JS value.

In fact, you may not necessary to use different page to display, you can use the same page, and thus, you just check, whether the JS value exist, if yes, use it, if no, use default.

HTH.

Nick
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas, do you mind to show me sample code on what you explain , i really new on javascript ? thank you , and hope you can help ..
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try this:

In abc.jsp,


Note that this is assume that, the 1st load of the page does NOT contain info for that CHECK button, so, if you really need to assign a checkbox value to the JSP variable, you do a submit to the SAME page.

After you do everything, you can click another submit.

HTH

Nick
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, nicholas, i finally get it ...i try on submit hidden as shown following code






but , let say if i want to save several js variable ...then how i implement array in my case ?

and for get.jsp page , to get array ...can i using code like


thank you !

[ October 22, 2004: Message edited by: Alvin chew ]

[ October 22, 2004: Message edited by: Alvin chew ]
[ October 22, 2004: Message edited by: Alvin chew ]
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the same variable name for those form objects. The container will translate the values into a String[].

Nick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic