• Post Reply Bookmark Topic Watch Topic
  • New Topic

applet-servlet communication  RSS feed

 
nesta fdb
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use java.net package to realize the commnunication between applet and servlet, but my programe can't run well.
Help,please.
Thanks in advance.
codes as follows:

applet codes:
public class TestApplet extends Applet {
URL url;
URLConnection urlcon;
int hello;
public void init(){
try{
hello=111;
String link="http://localhost:8080/examples/servlet/TestServlet";
url=new URL(link);
urlcon = url.openConnection();
urlcon.setDoOutput(true);
urlcon.setDoInput(true);
urlcon.setUseCaches (false);
urlcon.setDefaultUseCaches (false);
DataOutputStream dos=new DataOutputStream(urlcon.getOutputStream());
dos.writeByte(hello);
dos.flush();
dos.close();
}catch(ProtocolException pe){
pe.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
}
Servlet codes:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet
{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
System.out.println("hello");
DataInputStream in = new DataInputStream(request.getInputStream());
try {
int hello;
hello=in.readByte();
in.close();
System.out.println(" "+hello);
} catch (Exception exp) {
exp.printStackTrace();
}
}
}
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!