折腾两天了,就是不行,大侠帮帮忙!!!
我想完成applet到servlet的通讯,我是利用java.net.*,所提供的服务。
看了很多相关材料,而且感觉代码很容易理解,不知道为什么,我自己尝试,就是不行,请帮帮忙,先谢了。
applet程序:
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程序:
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();
}
}
}