Writing JSP code inside JS
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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");%>
The code is like that
var userName=<%request.getSession().getAttribute("userName");%>
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You're missing an equals sign, and a couple of double quotes:
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Suddhasattwa,
I am not very much sure but you can try this..
var userName=<%=request.getSession().getAttribute("userName");%>
regards,
PP
I am not very much sure but you can try this..
var userName=<%=request.getSession().getAttribute("userName");%>
regards,
PP
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Title says, you want to write jsp code inside .js file, is it?
[Servlet tutorial] [Servlet 3.0 Cook Book]
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The (String) cast is not necessary, as the object's toString method will be called automatically.
Quotes, on the other hand, are essential.
Quotes, on the other hand, are essential.
Kaizar Laxmidhar
Greenhorn
Posts: 10
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What does "is not working" mean? What is being generated?
Kaizar Laxmidhar
Greenhorn
Posts: 10
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 17 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
| Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










