Link to home
Start Free TrialLog in
Avatar of lar_jens
lar_jens

asked on

Creating a <form> element

In an HTML form, an element of that form will be included in the parameter list to the server when submit is pressed.

Like this:

<form>
<select name="selection">
<option>Choice A</option>
<option>Choice B</option>
<option>Choice C</option>
</select>
<input type="submit" value="submit">
</form>

When the user hits the Submit button, the server gets:
selection=Choice A (depends on the users choice)

Now to the question:
How do I write an applet that I can include in a <form> whose value is included in a submit?

-LJJ
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What do mean by the 'value' of the applet?
 Hmmm... I am not sure if you can do this, to include an Applet in your Servlet form. If you have data in your Applet that you need to pass it in the Servlet/JSP page, you can do it by creating a URLConnection, calling the Servlet through this URLConnection and passing the data to the Servlet using POST or GET. Then you can have a hidden field in your form that contains the values passed by the Applet.

  I am not sure if this is what you are asking though.
Avatar of lar_jens
lar_jens

ASKER

Well.. I want to be able to do something like:

<form method="post" action="url...">
<applet name="appletName" code="..."></applet>
<input type="submit" value="submit">
</form>

When submit is pressed I get:
appletName=result of user interaction with applet

-LJJ
 I see... But how will you send the values of the Applet (I guess you will have JTextField or something like this) to the server? I guess you need a means of communication and this can only be done with HTTP (if you want to talk to Servlet/JSP), via TCP/IP - sockets - if you want to talk to some application and via SMPT if you want to talk to a mail server. It is that simple, just put the applet tag within the form. You cannot just do it.

  Maybe someone else who has more experience can help us more.
> It is that simple, just put the applet tag within
the form.

 Sorry I meant: It is *not* that simple.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CEHJ

I have begun to think like this myself..
Going to test a few things, and then get back here with the obtained results..

:)
Another challenge:

I am using AWT with jdk1.1
Maybe this could work:

<script language="JavaScript">
function onSubmit() {
document.form1.input1=document.MyApplet.getWhatever();
document.form1.action="/bla.jsp"
document.form1.submit();
}
</script>

.
.
.
<applet name="MyApplet" code="..."></applet>
<form name="form1" action="javascript:onSubmit()">
<input type="hidden" name="input1">
<input type="submit">
</form>
That was what I was getting at pellep. But I wouldn't call form1.submit() in the submit handler itself.
1. create a raw socket connection to the web server where the servlet and the applet resides. (port 80 for http usually). Note: this will not work if the web server is not on the same machine where the applet originated due to the applet security restrictions

2. Follow the http protocol. (if POST is used)

 String hostName  ="127.0.0.1";
 int hostPort = 80;
 Socket sock;
try
          {    
               sock = new Socket(hostName, hostPort);
               System.out.println("connection ok");
               String RETURN = "" + (char)13 + (char)10;
               
               InputStream input;
               OutputStream output;
               input = sock.getInputStream();
               output = sock.getOutputStream();
               
               
               //String contentData = "WCI=invite&WCE=invForm&txtScreenName=ipc2002&txtPhoneNum=0123609742";
               String contentData = "file=&handphone=123";
               
               System.out.println("Starting to send username and password");
               Thread.sleep(2000);
               output.write( ("POST /servlet/Cropper? HTTP/1.0" + RETURN).getBytes() );
               output.write( ("From: asdsd@sadfd.com" + RETURN).getBytes() );
               output.write( ("User-Agent: HTTPTool/1.0" + RETURN).getBytes() );
               output.write( ("Content-Type: application/x-www-form-urlencoded" + RETURN).getBytes() );
               output.write( ("Content-Length: " + contentData.length() + RETURN).getBytes() );
               output.write( (RETURN).getBytes() );
               output.write( contentData.getBytes() );
               output.write( (RETURN).getBytes() );
               
               Thread.sleep(5000);
                             
          }
          catch(Exception e)
          {
               System.out.println(e);
          }


if this doesn't work mail me. I copied and pasted this late at night...might have missed some details...

aKa, welcome to Experts Exchange. For suggestions on posting comments vs. answers, take a look at the bottom of this page (and related links).

-corey
The comment by CEHJ/pellep provides a better solution to me.