File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Writing JSP code inside JS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Writing JSP code inside JS" Watch "Writing JSP code inside JS" New topic
Author

Writing JSP code inside JS

Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
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
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
You're missing an equals sign, and a couple of double quotes:


Ping & DNS - my free Android networking tools app
prash patil
Ranch Hand

Joined: May 26, 2008
Posts: 40
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

Joined: Aug 29, 2007
Posts: 212

Title says, you want to write jsp code inside .js file, is it?


[Servlet tutorial] [Servlet 3.0 Cook Book]
Kaizar Laxmidhar
Greenhorn

Joined: Feb 25, 2004
Posts: 10
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
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
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

Joined: Feb 25, 2004
Posts: 10
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

Joined: May 28, 2008
Posts: 52
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
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
What does "is not working" mean? What is being generated?
Kaizar Laxmidhar
Greenhorn

Joined: Feb 25, 2004
Posts: 10
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

Joined: Jan 10, 2002
Posts: 61606
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing JSP code inside JS