• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Writing JSP code inside JS

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Struts Framework in a User Management portfolio(Web Based) in JBOSS.I want to assign a JS variable from a JSP variable.,But it is not working can anyone help me.

The code is like that
var userName=<%request.getSession().getAttribute("userName");%>
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're missing an equals sign, and a couple of double quotes:
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suddhasattwa,

I am not very much sure but you can try this..
var userName=<%=request.getSession().getAttribute("userName");%>

regards,
PP
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Title says, you want to write jsp code inside .js file, is it?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

JSP expression should always contain string value where as getAttribute returns object therefore needs to cast to String. And semicolon (;) is not required at the end.
Hope this should work.
var userName=<%=(String)request.getSession().getAttribute("userName")%>

regards,
Kaizar
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The (String) cast is not necessary, as the object's toString method will be called automatically.

Quotes, on the other hand, are essential.
 
Kaizar Laxmidhar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf,

Semicolon at the end required only if it is JSP scriptlet that is <% ... %>
But not required if it JSP expression that is <%= ... %>

Regards,
Kaizar
 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
all the suugesstions so far is not working...Can anyone have any suggestion...Also if you have diifferent idea of assigning the value of JS in Struts framework please provide me..
Thanks
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does "is not working" mean? What is being generated?
 
Kaizar Laxmidhar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1. Define hidden element in jsp file and assign userName value

<html:form action="/SomeAction">

<html:hidden property="userName" value="<%=(String)request.getSession().getAttribute("userName")%>" />

</html:form>

2. in JS do the following

var vUserName = document.getElementsByName("userName");

regards,
Kaizar
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kaizar Laxmidhar:

1. Define hidden element in jsp file and assign userName value


Completely unnecessary!

If the original post had been paying attention, he would have seen that his question has already been answered!

He is simply generating invalid JavaScript syntax!

String literals in JavaScript must be quoted.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be clear here. There can be no direct interaction between JSP and JavaScript. JSP executes on the server in order to format the HTML page containing the JavaScript to be sent to the browser.

What's happening here is that the JavaScript is being dynamically generated. And it must be generated using valid syntax. You can easily see what is being sent to the browser by doing a View Source in the browser.

Those who are unclear on this may find this article helpful.
 
I want my playground back. Here, I'll give you this tiny ad for it:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic