Start an applet from a servlet
I have been searching on how to start an applet from a servlet. Everything on the web so far has been on the opposite, connecting to a servlet from an applet. I am writing a gwt/j2ee app and need to post data to a servlet, then have the servlet start an applet and pass ser开发者_StackOverflowialized objects to the applet. The applet would then send data back the the servlet. Any ideas? Thanks in advance.
You don't and can't start an applet with a Servlet. You just let the applet during its init()
call the servlet for any data the applet needs and have the servlet return the desired data. Applet-Servlet communication can be done with help of a HTTP client in the applet. The basic Java SE API offers you java.net.URL
and java.net.URLConnection
for this.
InputStream response = new URL(getCodeBase(), "servletURL").openStream();
// ...
Here, servletURL
should match the url-pattern
of the servlet as you definied in the web.xml
, e.g. /servletURL
or /servletURL/*
.
See also:
- How to fire and handle HTTP requests with
URLConnection
An applet is started by the browser reading the HTML specifying the applet, downloading the applet's code, and running it.
Your servlet just needs to serve up HTML describing the applet, in the normal way - and then either serve the code as well, or let that be downloaded from a static site (if you see what I mean). Basically just remember that the servlet is there to serve data to the client. Think about what data the client needs in order to start the applet - and serve that data.
精彩评论