Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing jsp array variable in javascript

 
rammie singh
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through below code , it will help you.

 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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...
 
Anand Kumar Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe sesion is an implicit jsp object, can it be used inside a java method like that?
 
rammie singh
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
can any one please advice me on above problem..i am really facing this problem.
thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use like this:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic