Applet to JSP Communication

Hi, I have managed to info from JDBC to an applet using JSP as an intermediator. My next task is to update a record. I am wondering how to send the fields of an applet to a JSP page. Any hints would be much appreciated.James
[252 byte] By [jpoz] at [2007-9-26 1:31:42]
# 1

The simplest way is to use the showDocument method of your Applet's AppletContext to pass the input to the JSP in a GET request:String req = "some.jsp?var1=val1&var2=val2&var3=val3";

getAppletContext().showDocument(req, targetWindow);

How complex is the data you're needing to send?

cafal at 2007-6-29 1:31:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

The data to be passed back and forth are strings from a MSAccess DB. The JSP page loads the applet(which has <param> tags for the data that will be obtained after the user enters the criteria) inside of it . When the user enters the data and presses "search" it will send the data back to the JSP "page" which will perform the query. When the JSP has the info it sends it to the applet for display.

Questions:

Should I use two identical JSP pages to send the info back and forth or is it possible to do it on one page?

Regarding the code you sent, is this in the applet and do I use getParameter() in the JSP page to get the new parameter for the db query?

thanks

James

jpoz at 2007-6-29 1:31:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You can do it all in one page; do the request.getParameter() call in your JSP and see if you have a key value (say, "search"); if so, retrieve the rest of your data and then perform the search. Write the code to set the applet parameters based on whether or not the search was performed.

The getAppletContext().showDocument(URL) method does take place in the Applet, and I'm afraid I represented it to you incorrectly:String s = "some.jsp?var1=val1&var2=val2&var3=val3";

URL url = new URL(s);

getAppletContext().showDocument(url);

//or getAppletContext().showDocument(url, windowNameAsString);

cafal at 2007-6-29 1:31:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Ok, one more. What about passing an array back to the applet's scrolling list after JSP completes a wild card search'*'?
jpoz at 2007-6-29 1:31:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

That can be a little more complicated; your JSP will be sending info to the Applet by setting <PARAM/> tags in the body of the page, and you have to put all of your array data in one or more of these tags.

I'm not sure just how long the VALUE attribute of the tag is allowed to be, but it's relatively long. Place all of your data for your array into a single String with some delimiter (a comma in this example):<PARAM NAME="SomeVal" VALUE="one,two,three,four,five"/>

Then, in your applet, pull out the parameter and create your array with a StringTokenizer:String s = "";

String[] vals;

public void init(){

s = getParameter("SomeVal");

StringTokenizer st = new StringTokenizer(s, ",");

vals = new String[st.countTokens()];

int count=0;

while(st.hasMoreTokens()){

vals[count] = st.nextToken();

count ++;

}

}

That will pull your array into your Applet. This will not work for complex data, but should handle Strings and all primitive types (you can convert the Strings to whatever type you need using the appropriate wrapper-class methods.) If you're just building a drop-down, this should work fine.

cafal at 2007-6-29 1:31:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Thanks for all the help!
jpoz at 2007-6-29 1:31:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...