Chinaunix首页 | 论坛 | 博客
  • 博客访问: 619749
  • 博文数量: 98
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 1528
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-28 16:20
文章分类

全部博文(98)

文章存档

2011年(1)

2010年(11)

2009年(44)

2008年(42)

我的朋友

分类: Java

2008-04-25 15:39:53

/DaytimeApplet.java

      import java.applet.*;

      import java.awt.*;

      import java.io.*;

      import java.util.*;

      import java.net.URL;

      import java.net.Socket;

      import cuug.HttpMessage;

 

      public class DaytimeApplet extends Applet{

     

            TextField httpText,httpObject,SocketText,SocketObject;

            Button refresh;

            static final int DEFAULT_PORT = 1313 ;

     

            private int getSocketPort(){

                  try{ return Integer.parseInt(getParameter("socketPort"));}

                        catch(NumberFormatException e) {return DEFAULT_PORT; }

            }

            public void init(){

           

                  setLayout(new BorderLayout());

           

                  Panel west = new Panel();

                  west.setLayout(new GridLayout(4,1));

                  west.add(new Label("HTTP text:",Label.RIGHT));

                  west.add(new Label("HTTP Object:",Label.RIGHT));  

                  west.add(new Label("SOCKET text:",Label.RIGHT)); 

                  west.add(new Label("SOCKET Object:",Label.RIGHT));

                  add("West",west);

           

                  Panel center = new Panel();

                  center.setLayout(new GridLayout(4,1));

           

                  httpText = new TextField();

                  httpText.setEditable(false);

                  center.add(httpText);

           

                  httpObject = new TextField();

                  httpObject.setEditable(false);

                  center.add(httpObject);

           

                  SocketText = new TextField();

                  SocketText.setEditable(false);

                  center.add(SocketText);

           

                  SocketObject = new TextField();

                  SocketObject.setEditable(false);

                  center.add(SocketObject);

           

           

                  add("Center",center);

           

           

                  Panel south = new Panel();

                  refresh = new Button("Refresh");

                  south.add(refresh);

                  add("South",south);

      }

     

            public void start(){

                  refresh();

            }

     

            private void refresh(){

           

                  httpText.setText(getDateUsingHttpText());

                  httpObject.setText(getDateUsingHttpObject());

                  SocketText.setText(getDateUsingSocketText());

                  SocketObject.setText(getDateUsingSocketObject());

            }

           

            private String getDateUsingHttpText(){

                  try{

                        URL url = new URL(getCodeBase(),"/servlet/DaytimeServlet");

                        HttpMessage msg = new HttpMessage(url);

                 

                        InputStream in = msg.sendGetMessage();            

                        DataInputStream result =

                              new DataInputStream(new BufferedInputStream(in));

                        String date = result.readLine();                

                        in.close();               

                        return date;

            }

            catch (Exception e){

                  e.printStackTrace();

                  return null;

            }

      }

     

      private String getDateUsingHttpObject(){

            try{

                  URL url = new URL(getCodeBase(),"/servlet/DaytimeServlet");

                  HttpMessage msg = new HttpMessage(url);

                 

                  Properties props = new Properties();

                  props.put("format","object");

                 

                  InputStream in = msg.sendGetMessage(props);

                  ObjectInputStream result = new ObjectInputStream(in);

                 

                  Object obj = result.readObject();

                  Date date = (Date)obj;

                 

                  return date.toString();

            }

            catch (Exception e) {

                  e.printStackTrace();

                  return null;

            }

      }

     

      private String getDateUsingSocketText(){

            InputStream in = null;

            try{

                  Socket socket = new

                        Socket(getCodeBase().getHost(),getSocketPort());

                  PrintStream out = new PrintStream(socket.getOutputStream());

                  out.println();

                  out.flush();

                 

                  in = socket.getInputStream();

                  DataInputStream result = new DataInputStream(new

                              BufferedInputStream(in));

                  String date = result.readLine();

                 

                  return date;

            }

            catch (Exception e)

            {

                  e.printStackTrace();

                  return null;

            }

            finally{

                  if (in!= null){

                        try { in.close(); }

                        catch (IOException ignored){}

                  }

            }

           

      }

     

      private String getDateUsingSocketObject(){

            InputStream in = null;

            try{

                  Socket socket = new

                        Socket(getCodeBase().getHost(),getSocketPort());

                 

                  PrintStream out = new PrintStream(socket.getOutputStream());

                  out.println("object");

                  out.flush();

                 

                  in = socket.getInputStream();

                  ObjectInputStream result = new ObjectInputStream(new

                        BufferedInputStream(in));

                 

                  Object obj = result.readObject();

                  Date date = (Date)obj;

                 

                  return date.toString();

            }

            catch (Exception e)

            {

                  e.printStackTrace();

                  return null;

            }

            finally{

                  if (in!= null){

                        try { in.close(); }

                        catch (IOException ignored){}

                  }

            }

           

           

      }

     

     

      public boolean handleEvent (Event event) {

           

            switch (event.id){

                  case Event.ACTION_EVENT:

                        if (event.target == refresh ){

                              refresh();

                              return true;

                        }

            }

            return false;

      }

}   

 

在这个appletgetDateUsingSocketText()和getDateUsingSocketObject()方法分别通过普通的套接口和套接口中传输对象的方法来获得服务器的时间。在这两个方法中用appletgetCodeBase().getHost方法来获得主机名,端口号要取决于servlet,因此在servet中用getSocketPort()来获得服务器的套接口的端口号。

一旦连接建立,appletservlet通过一种未命名的协议来通信,本例中的协议非常简单,applet发送一行信息表明其想通过text方式还是通过对象方式来获得,如果applet发送的这行信息是“ojbect”,则servlet通过对象方式返回服务器的日期,如果applet发送的这行信息是其他内容,则通过text方式来返回服务器的日期。

 

阅读(469) | 评论(0) | 转发(0) |
0

上一篇:Servlet高级编程部分5

下一篇:旅游知识2

给主人留下些什么吧!~~