This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes accessing jsp array variable in javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "accessing jsp array variable in javascript" Watch "accessing jsp array variable in javascript" New topic
Author

accessing jsp array variable in javascript

rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Hi
i am having an array of objects in a jsp page.
i want to access that array in a function in javascript in the same jsp page.
could any one please tell me how to do that
regards
thanks.

Anand Kumar Singh
Greenhorn

Joined: Oct 18, 2007
Posts: 22
Go through below code , it will help you.



SCJP 5, SCWCD 5, SCBCD 5,
SCEA 5,
OCUP Fundamental,
OCUP Intermediate,
OCUP Advanced,
IBM Certified Solution Designer - Object Oriented Analysis and Design, vUML 2
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17



Are you expecting that this will set a session attribute?? If yes, then I'm afraid you are wrong. You cannot set a session attribute through javascript directly. Session is stored on the server, so you'll have to contact the server to be able to set a session attribute...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Anand Kumar Singh
Greenhorn

Joined: Oct 18, 2007
Posts: 22
You can do it using JSTL also.

Go through below code , it will help you.



In above code CustomFieldDetails is a String of objects which is set in request object.
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Is there any other way we can solve this problem.

i also want to send the selected object to my servlet ManageUser.java
can any body help me.
actually i am very new to this servlet and jsp.....so any simple way will be highly apreciated from my side.

regards
thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

rammie you'll have to make either an AJAX request to the server for that or submit the page. Can you please elaborate what you want to accomplish so that I could, well, write some code for you. I would also like to suggest you that you read a couple of books because it seems that you are very new to Servlets and JSP. That way you can understand things better ...
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Hi ankit
thanks a lot for your support.
actually my main moto is.....

i have a jsp page searchedEmpPage.jsp
in this as it there in code a list of employees is displayed .These employee would be as object which are stored in an array fetchedArrayObject[] as mentioned there in my jsp page.

now i want to select one employee through radio button....and which ever employee(object) is selected through radio button ......that element(object) has to be passed to servlet.

my servlet here is ManageUser.java



i hope you got my problem.

regards
thanks

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

You need to STOP right now and learn how servlets, JSP and HTML work. What you are asking is nonsense. You can't mix Java statements with JavaScript, you can't "call" a servlet without submit a form or making an Ajax request, and you can't communicate with code in the JSP once it's executed.

This has been explained to you in your other posts and you are just wasting everyone's time, including your own, by posting the same mistakes over and over again.

For starters, you might want to read this article to get started on understanding how JSP works. Then find yourself a good book or tutorial.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Okay I know that this might seem wrong but I'm providing a complete solution to your problem.

First of all I hope that there is something like ID in your employee object which is represented as getEmpNumber i.e. the value in it is unique.

There were two solutions in my mind so I'm giving one of them (I know my code is very dirty but this is just a quick fix). Now here is the JSP



I think that should do it for the JSP but I've not tried running it. The server side code will be simple



Now you can do anything with these values in your servlet...
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Thanks Ankit
thanks a lot i think this must solve my problem and sorry for all trouble.
thanks for the reference link also.
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
hi
sorry to disturb you guys
problem i am facing is ...i am unable to use sesssion.setAttribute() inside a function....here setSession function

Since i am using netbeans the error being shown is

canot find symbol
symbol : variable session
location : class SimplifiedJSPServlet





my error is coming at this line
session.setAttribute("sendToRequestHandler",fetchedArrayObject[i]);
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1400
    
    6

I believe sesion is an implicit jsp object, can it be used inside a java method like that?


Swastik
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Hi
yaah it's not working

but what is the way we can put an object in a session in a function in a jsp page.
can any body help me please.

with regards
thanks
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Hi
can any one please advice me on above problem..i am really facing this problem.
thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

rammie singh wrote:but what is the way we can put an object in a session in a function in a jsp page.

This is where you are going wrong. Do you want to affect the session as part of the JSP processing before the page gets sent to the browser, or in JavaScript after the page gets sent to the browser?

If the latter (which is what I suspect), then the fact that this is a JSP is completely irrelevant. Once the page gets to the browser, it's just an HTML page like any other.

Did you read the article that I linked to? Did you understand it?
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Hi every body
i am sorry to say that but can i really get the solution of my problem in this forum

well as far as Mr. ankit garg solution is concerned....it is not working......the error is same...the function setVals(i)
is not able to access the arrays you have created above. because function loading() is not able to push the values from array fetchedArrayObject[] to created array in javascript.

the same problem i am asking from last three days....that whether it's a javascript function....that is a function defined within tag <script type=text/javascript> ....... </script> of a jsp page or a function defined within the tag <body> ...</body> of a jsp page, how can we acccess a jsp variable within those function directly. and if yes the how.

i don't know if this forum runs to give any solution or just for fun.
i am sorry guys if i made any hard coment.
thanks.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
JSP/Java runs at server side. JSP/Java generates HTML/CSS/JS output. Server side sends output to client side. Client side runs HTML/CSS/JS.

View the page source in your webbrowser and think once more about it.

To "access" JSP/Java variables in JS, simply let JSP/Java print it to the output as a JS variable!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

For the last time, READ THIS ARTICLE. If you are going to ignore the advice and information presented to you, of course you aren't going to learn anything.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

rammie singh wrote:i don't know if this forum runs to give any solution or just for fun.


You have been given plenty of information that you seem to be just ignoring. Are you posting for fun or are you actually trying to learn anything?
Lei Guoguo
Greenhorn

Joined: Mar 31, 2009
Posts: 26
you can use like this:



I'm a chinese, my english is so bad, I need help. If you want to help me, you can send your question to my e-mail: lgg860911@yahoo.com.cn, I will help you, and improve my english. Thanks everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing jsp array variable in javascript
 
Similar Threads
when i retrive data from access it showing empty page in tomcat
how to preserve the lastly added contents in table in JSP page?
struts-2 (set values to action class)
Problem in using date in WHERE clause in SELECT query in Servlet.
spaces while populating using s:iterator