This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing JSP code inside JS

 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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");%>
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're missing an equals sign, and a couple of double quotes:
 
prash patil
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Title says, you want to write jsp code inside .js file, is it?
 
Kaizar Laxmidhar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic