sendApplet.java
import java.applet.Applet;
import java.awt;
import java.net.*;
import java.io.*;
public class sendApplet extends Applet
{
Lable sendlable;
Lable messageinfor;
Button sendButton;
TextField messageText;
TextField sendText;
URLConnection connect;
String message;
public String getAppletInfo()
{
return "sendApplet - Applet to communicate with receiveServlet , Written by ding;";
}
public synchronized void init()
{
super.init();
resize(500,300);
sendlable = new Lable("Please Enter Massage");
messageInfor = new Lable("What you have send:");
sendButton = new Button("Send");
sendText = new TextField(40);
messageText = new TextField(40);
messageText.setEditable(false);
//下面实现一个Panel
Panel mainp = new Panel();
GridBagLayout gbl = new GridBaglayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0;
gbc.weighty = 0;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight= 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
mainp.setLayout(gbl);
gbl.setConstrains(sendable,gbc);
mainp.add(sendlable);
gbc.gridy = 1;
gbc.gridwidth = 9;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbl.setConstraints(sendText,gbc);
mainp.add(sendText);
gbc.gridx = 9;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbl.setConstraints(sendButton,gbc);
mainp.add(sendButton);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 10;
gbl.setConstraints(messageinfo,gbc);
mainp.add(messageInfo);
gbc.gridy = 3;
gbc.weighty = 100;
gbc.gridheight = 10;
gbc.fill = GridBagConstraints.BOTH;
gbl.setConstraints(messageText,gbc);
mainp.add(messageText);
//把实现的Panel加入Applet
setLayout(new BorderLayout());
Add("Center",mainp);
chatURL = getCodeBase();
}
public synchronized void start()
{
}
public synchronized void stop()
{
}
public synchronized void destroy()
{
}
//方法SEND 把用户在applet 中输入的字符串送给Servlet
//然后把Servlet 的处理结果显示出来
Private void Send()
{
message = sendText.getText();
//清除用户的输入信息
sendText.setText("");
showStatus("Message send!");
//把输入的字符串转化为 x-www-form-urlencoded 格式
String queryString = "/servlet/ReceiveServlet?message="+URLEncoder.encode(message);
p("Attempting to send:"+message);
//建立与Servlet的联接,并取得Servelt的输出信息
try
{
connect = (new URL(chatURL,queryString)).openConnection();
showStatus("open connection!");
//下次连接不用Cache
connect.setDefaultUseCaches(false);
//这次连接也不用Cache
connect.setUseCaches(false);
//打开?流用于读数据
connect.setDoInput(true);
//不能用于写数据
connect.setDoOutput(false);
//服务器与客户的真正连接
connect.connect();
p("Made connection to "+connect);
showStatus("Open Stream!");
DataInputStream in = new DataInputStream(connect.getInputStream());
showStatus("reading!");
message = in.readLine();
while (message! = null)
{
messageText.setText(message);
message = in.readLine();
}
}
catch(MalformedURLException e2)
{
System.err.println("MalformedURLException!");
e2.printStackTrace(System.err);
showStatus("MalformedURLException!");
}
catch(IOException e1)
{
System.err.println("IOException!");
e2.printStackTrace(System.err);
showStatus("IOException");
}
}
}
//applet 对用户的动作作处理。
public boolean action(Event evt,Object arg)
{
if (evt.target == sendButton)
{
Send();
return true;
}
return super.action(evt,arg);
}
private void p(String debug)
{
System.err.println("app-serv-let:"+debug);
}
}
///////////////////////////////////////////
receiveservlet.java
import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
public class receiveservlet extends HttpServlet
{
public void service (HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/plain");
ServletOutputStream out = res.getOutputStream();
out.print("receive user message:");
out.print("req.getParameter("message"));
}
public String getServletInfo()
{
return "return a String representation of the current time";
}
}