Applet To Servlet Communication

java packages » java.applet
Java Example Program / Sample Source Code
Servlet
import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

public class AppletToServlet extends HttpServlet {
// Getting a String object from the applet and send it back.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
InputStream inputStream = request.getInputStream
();
ObjectInputStream inputFromApplet =
new ObjectInputStream(inputStream);
String string =
(String) inputFromApplet.readObject();
// getting string value and passing to applet
OutputStream outputStream = response.getOutputStream();
ObjectOutputStream objectOutputStream =
new ObjectOutputStream(outputStream);
objectOutputStream.writeObject
(string);
objectOutputStream.flush
();
objectOutputStream.close
();
} catch (Exception e) {
e.printStackTrace();
}
}
}
web.xml
<web-app>
    <display-name>AppletToServlet</display-name>
    <description> AppletToServlet</description>
        <servlet>
            <servlet-name>applettoservlet</servlet-name>
            <servlet-class>AppletToServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>applettoservlet</servlet-name>
            <url-pattern>/applettoservlet</url-pattern>
        </servlet-mapping>
</web-app>
Html Code
<html>
    <head>
        <title>AppletToServletExample</title>
    </head>
    <body>
        <applet code="AppletToServletExample" width="700" height="500"></applet>
    </body>
</html>

Applet


AppletContext

2011-2012 JavaExamples.org Privacy Policy Powered by Google App Engine