aspose file tools*
The moose likes JSP and the fly likes How to pass Java Script variable to JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass Java Script variable to JSTL" Watch "How to pass Java Script variable to JSTL" New topic
Author

How to pass Java Script variable to JSTL

Kiran Gaddam
Ranch Hand

Joined: Aug 05, 2011
Posts: 35
    
    1
Hello all..

is their any way that I can assign JavaScript variable to JSTL or use in JSP.

My Requirement is,
I should greet user according to time... For that I should get user machine time(not server time)... for that I should use JavaScript.

Every thing is fine,,, But the catch is I am using spring Internalization for messages.

So I should get the client machine time and greet using spring spring internalization.

Any ideas please...
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1026
No, there is no direct way to assign javascript variable to JSTL/JSP.

Java runs on the server, produces an HTML page.
Javascript runs on the client.

The only way to pass information from client to server is to make an HTTP request - which is the basis of ajax.

My suggestion for a workaround would be to do your i18n in Spring, providing the message "The time is currently [insertTimeHere]" or "It is [insertTimeHere] where you are right now"
That message can get output onto the html page in a javascript variable.
You then get the current time from the client, and use string functions to replace [insertTimeHere] with the time from the client.

So you will be able to localize the message part of "The time is currently..." with Spring but not the actual time string (got from the client)
Does that suffice?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Please read this article to understand how JSP operates and why that is impossible.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kiran Gaddam
Ranch Hand

Joined: Aug 05, 2011
Posts: 35
    
    1
Thanks for you Reply Bibeault, Evans..

But still I am struggling with the requirement. Any more ideas are welcome...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Well, if you read and understood the article it would be clear to you that it's impossible for Javascript to affect processing which was done at an earlier time on a different computer. But that doesn't necessarily mean that you can't fulfil your requirement; it just means you can't do it the way you originally thought. So put that idea out of your mind and consider the actual situation.

You have an HTML page in a browser. And you want to take the time from the computer where the browser is running, and have Spring format that time according to the user's I18N rules. So how to do that? Here's what you want to do, approximately:

(1) Get the time from the browser using the well-known Javascript function.
(2) Send it to Spring to be formatted according to the user's rules.
(3) Receive the formatted version and display it in the right place in the HTML.

Does that suggest something which you can implement?
Kiran Gaddam
Ranch Hand

Joined: Aug 05, 2011
Posts: 35
    
    1
ok...got it... I will make AJAX call to spring controller..

first I need to send time from javascript to spring controller through an AJAX call
take that value and process it in java.
send it back to JSP

Will put the code once I am done..
may help some others

Thanks for your time and response guys
Kiran Gaddam
Ranch Hand

Joined: Aug 05, 2011
Posts: 35
    
    1
I found an other way around for the issue...

it is ...




just dont forget to include jquery library...

Thats it...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

That's even better than using AJAX.

(When I was travelling in South America I noticed that people didn't start saying "Buenas tardes" -- Good afternoon -- until at least 2 o'clock in the afternoon. And they didn't say "Buenas noches" -- Good evening -- until at least 8 PM. Does your Spring internationalization take that sort of cultural factor into account?)
Kiran Gaddam
Ranch Hand

Joined: Aug 05, 2011
Posts: 35
    
    1
Thats a good point Paul..

In this case I was told to use these timings..
and we are not taking cultural factors into account.

Again, that is an interesting point you made
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass Java Script variable to JSTL