aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Head First Servlets & JSP book - JSP Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Head First Servlets & JSP book - JSP Question" Watch "Head First Servlets & JSP book - JSP Question" New topic
Author

Head First Servlets & JSP book - JSP Question

Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
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

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
But you could generate javascript with JSP, so you could assing variables on the server side.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

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

Joined: Aug 04, 2004
Posts: 724
Absolutely right Nicholas
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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

Joined: Jan 08, 2004
Posts: 834
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

Joined: Aug 04, 2004
Posts: 724
You will need a form and the variables will be submitted with GET or POST.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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

Joined: Jan 08, 2004
Posts: 834
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

Joined: Nov 07, 2003
Posts: 4982
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

Joined: Jan 08, 2004
Posts: 834
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

Joined: Nov 07, 2003
Posts: 4982
You can use the same variable name for those form objects. The container will translate the values into a String[].

Nick
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Servlets & JSP book - JSP Question
 
Similar Threads
floating image in firefox ? still in trouble ^_^
anyone face Error - tag.getAsString : component context is not defined?
What happen to this Tabbed form ?
Struts - display data PROBLEM
Struts validation issue: Cannot load a validator resource