aspose file tools*
The moose likes Applets and the fly likes Jsp--Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Jsp--Applet" Watch "Jsp--Applet" New topic
Author

Jsp--Applet

Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
Hi, I am working with an Applet/Jsp.
The applet only has two Texfield and a Boton.
First, i am calling the applet from a .jsp with this tag:

so, when i call the .jsp from the browser, it displays the applet with no problem.
As you can see i am calling another jsp(result.jsp), so that when i enter two numbers or letters,etc.. in the Texfields in the applet, this .jsp will receive those parameters and print them in screen.

The problem is that right now is like: there is no comunication, because when I press the button nothing happens.

is this the right way to do it?
what can i do?

the jsps:






[ May 06, 2007: Message edited by: Jack Bolton ]
[ May 06, 2007: Message edited by: Jack Bolton ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
No, this is not the right way to do it. Fields in applets are not part of the web page or any of its forms. While it's possible to achieve some limited communication between an applet and the web page it's on (using a technology called LiveConnect), I would advise against using that.

But the applet can instruct the browser to replace the web page with a different one, via the getAppletContext().showDocument(String URL) call. That URL can include the two field values as GET parameters.


Ping & DNS - my free Android networking tools app
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
Originally posted by Ulf Dittmer:

But the applet can instruct the browser to replace the web page with a different one, via the getAppletContext().showDocument(String URL) call. That URL can include the two field values as GET parameters.


in don't undesrstand well what you said.
Do you have examples?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Let's say the parameter are names param1 and param2, and you want to submit to http://www.server.com/myApp/myServlet. The code to do this would be:

where 'applet' is a reference to the applet object (which you can leave out if this code is part of the Applet class).
[ May 07, 2007: Message edited by: Ulf Dittmer ]
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
I am working with Blazix Web Server to do this.
This is my applet class.
I am only working with the two .jsps and this class.
Where should I "insert" the code you posted?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
In the "actionPerformed" method, because that's the one that gets called when you click the button. And since that's not the one extending Applet, you'll need to pass a reference to the Sum class in the constructor.
[ May 08, 2007: Message edited by: Ulf Dittmer ]
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
i am declaring like this:


and i get: "unreported exception java.net.MalformedURLException; must be caught or declared to be thrown"

what should i do?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
A number of things could go wrong when trying to access an URL, which is why that statement can throw an exception. Surround the statement with a try/catch block, and within the catch, somehow alert the user that there was a problem.
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
ok,
I declared it like this:



but two things I don't understand:
1. in the code you posted:


what "param1" and "param2" represent?

and 2.
Originally posted by Ulf Dittmer:
And since that's not the one extending Applet, you'll need to pass a reference to the Sum class in the constructor.



do you mean I have to make a reference of the actionPerformed method in the main method of the Sum class?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
try{
URL url = new URL("http://localhost:81/5/test.jsp" +s.getText()+e.getText());

}
catch(MalformedURLException e){

}


It is generally not a good idea to ignore exceptions, and definitely not here.

URL url = new URL("http://www.server.com/myApp/myServlet?param1=" +textfield1.getText()+"¶m2="+textfield2.getText());

what "param1" and "param2" represent?

You can choose those freely - whatever makes sense for your server program. But the server app will need to know what they are so it can access them. The JSP page can't use "s.getText" (which you have above), instead it needs to access the request object and get those parameter. To do that, it needs to know their names.

do you mean I have to make a reference of the actionPerformed method in the main method of the Sum class?

No. The actionPerformed method is in the ArcControls class, and that's where it is used. But that part of the code is already in place, so you don't need to worry about it.

It's a bit strange to have an applet with a main method, but I'm assuming you're aware of the differences between applets and applications.
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
Originally posted by Ulf Dittmer:
You can choose those freely - whatever makes sense for your server program. But the server app will need to know what they are so it can access them. The JSP page can't use "s.getText" (which you have above), instead it needs to access the request object and get those parameter. To do that, it needs to know their names.


give me a code example of this please...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
give me a code example of this please...


Um, example for what? The various snippets I posted contain just about everything you'd need to make this work. Is there something in particular you're having trouble with?
[ May 20, 2007: Message edited by: Ulf Dittmer ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jsp--Applet