1 ) You can't call any of the JSP beans directly from JavaScript. But it is pretty
simple to convert data returned from a bean property to JavaScript. Provided that
the data can be represented as a string and is not too large.
See my example below for converting a java array to javascript array.
2 ) Pesonally I thinks it's a good idea to keep the servlet as light weight as
possible. To keep the servlet functionality separate from the back end (what ever
it
may be). But you do not save and strain on the webserver. The sevrlet and the bean
are all running on the same VM.
Peter
Example of passing a Java array to a JS array vis JSP
jsp1.jsp
====================
<HTML>
<HEAD>
<jsp:useBean id="Jsp1Id" scope="session" class="untitled32.Jsp1Bean" />
<TITLE>
Jsp1
</TITLE>
</HEAD>
<BODY>
<H1>
JBuilder Generated JSP
</H1>
<FORM method="post">
<INPUT name = xx>
<SCRIPT LANGUAGE ="JavaScript">
ar = new Array(<%= Jsp1Id.getArray() %>)
document.forms[0].xx.value=ar[0]
</SCRIPT>
</FORM>
</BODY>
</HTML>
=======================
jsp1Bean.java
=======================
package untitled32;
public class Jsp1Bean {
static String[] data = {"aaa1","bbb2","ccc3"};
public String getArray() {
int i=0;
String array="";
while (i!= data.length) {
array += "'" + data[i] + "'";
i++;
if (i != data.length) {
array += ",";
}
}
return array;
}
=================
O'Keeffe Patrick wrote:
> Sorry to bother you, but I'm a having few problems with JSP and Beans:
>
> 1) I would like to populate an array stored on the client with data from a bean.
> Is there any way call a getXXX() method within a JavaScript function, or do I
> have to pass the result of the getXXX() method as a parameter to the JavaScript
> function? Also, is there any way of getting at session variables within
> JavaScript?
>
> 2) Bean / servlet design: what are the advantages and disadvantages of servlets
> and beans calling backend logic (i.e. database/ RMI client). These are the two
> scenarios:
>
> A: The servlet contacts the backend logic, passes the result to the bean via its
> setXXX() method and then passes control to the JSP.
>
> B: The bean contains an 'initData()' method which is called by the servlet. This
> method instructs the bean to contact the backend logic and use the result to set
> its own properties. The servlet then passes control to the JSP. Here the servlet
> is acting solely as a controller.
>
> I like B, because it keeps the servlet as thin as possible, putting less strain
> on the web service. But is it good design to let the bean set its own
> properties?
>
> Also, do beans run in the same JVM as the servlets?
>
> Any thoughts on the above would be most helpful.
>
> Many thanks,
>
> Patrick O'Keeffe
>
> Tertio Limited - One Angel Square, Torrens Street, London EC1V 1NY
> Tel: +44 (0)171 843 4000 Fax: +44 (0)171 843 4001 Web http://www.tertio.com
> Any views expressed in this message are those of the individual sender,
> except where the sender specifically states them to be the views of Tertio Ltd.
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> FAQs on JSP can be found at:
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html