Accessing a jsp:usebean from javascript...?

  • Thread starter Brice Laurencin
  • Start date
B

Brice Laurencin

My code is like that :

<jsp:useBean id="vConf" type="cat.eur.tb.eleccard.datatypes.Config[]"
scope="session" />
<jsp:useBean id="equNo" type="java.lang.Integer" scope="session" />

<script language="javascript">
function submit_form()
{
var equ = vConf[equNo].getEqu();
(...)
}

The problem is that both vConf and equNo are not known. I tried with
<%=...%>
and it is still inaccessible.

It is correct ? can I do it ? Where I am wrong ?
 
K

kaeli

My code is like that :

<jsp:useBean id="vConf" type="cat.eur.tb.eleccard.datatypes.Config[]"
scope="session" />
<jsp:useBean id="equNo" type="java.lang.Integer" scope="session" />

The bean is interpreted by the server (servlet engine). The browser sees
no such thing. View the HTML source to see what the browser sees.
Javascript, as you are using it, runs on the client, well after the
server is done. It cannot call bean methods.
<script language="javascript">
function submit_form()
{
var equ = vConf[equNo].getEqu();

If you want the value of that method in a js variable, assign it at run
time.
var equ = <%= vConf[equNo].getEqu() %>;
If the above method returns a string, put it in quotes.
var equ = "<%= vConf[equNo].getEqu%>";

The above assumes both beans are written properly and their methods
return what is expected. If you aren't sure, ask in
comp.lang.java.programmer

--
--
~kaeli~
To steal ideas from one person is plagiarism; to steal from
many is research.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
T

Thomas 'PointedEars' Lahn

Brice said:
<jsp:useBean id="vConf" type="cat.eur.tb.eleccard.datatypes.Config[]"
scope="session" />
<jsp:useBean id="equNo" type="java.lang.Integer" scope="session" />

<script language="javascript">

The "language" attribute is deprecated since
HTML 4, but the "type" attribute is required:

function submit_form()
{
var equ = vConf[equNo].getEqu();
(...)
}
[...]

It is correct ?
No.

can I do it ?
Possibly.

Where I am wrong ?

You are using client-side JavaScript here. Client-side code cannot
access server-side code (directly). JavaServer Pages generates content
temporarily in the server's memory by replacing markup in the file
(check out "view-source:") and sends that composed data to the client
which interprets it. You can only access the *replaced* content with
client-side scripting through the DOM of the UA, unless the server-side
application provides an API for server-side JavaScript.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top