servlet与applet通信示例

servlet

 1 import  java.io. * ;
2 import java.util.Date;
3 import javax.servlet. * ;
4 import javax.servlet.http. * ;
5 publicvoid Riceive extends HttpServlet
6 {
7 publicvoid service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
8 {
9 res.setContenType( " text/plain " );
10 ServletOutputStream out = res.getOutputStream();
11 out.print( " recive user message: " );
12 out.print(req.getParameter( " message " ));
13
14
15 } // end service
16
17 } // end receive

applet

  1 import java.applet.Applet;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.net.*;
5 import java.io.*;
6
7 publicclass sendapplet extends Applet implements ActionListener
8 {
9 Label sendlable;
10 Label messageInfo;
11 Button sendButton;
12 TextField sendText;
13 TextField messageText;
14 URLConnection connect;
15 String message;
16 URL chatURL;
17 public String getAppletInfo()
18 {
19 return"this is a applet--servlet";
20 }
21
22 publicsynchronizedvoid init()
23 {
24 super.init();
25 resize(500,300);
26 sendlable=new Label("Please enter Massage");
27 messageInfo=new Label("What you hava send:");
28 sendText=new TextField(40);
29 sendButton=new Button("Send");
30 messageText=new TextField(40);
31 messageText.setEditable(false);
32 //
33
34
35
36
37 //
38 Panel mainp=new Panel();
39 GridBagLayout gbl=new GridBagLayout();
40 GridBagConstraints gbc=new GridBagConstraints();
41 gbc.weightx=0;
42 gbc.weighty=0;
43 gbc.gridy=0;
44 gbc.gridx=0;
45 gbc.gridwidth=10;
46 gbc.gridheight=1;
47 gbc.anchor=GridBagConstraints.CENTER;
48 gbc.fill=GridBagConstraints.NONE;
49 mainp.setLayout(gbl);
50 gbl.setConstraints(sendlable,gbc);
51 mainp.add(sendlable);
52 gbc.gridy=1;
53 gbc.gridwidth=9;
54 gbc.fill=GridBagConstraints.HORIZONTAL;
55 gbl.setConstraints(sendText,gbc);
56 mainp.add(sendText);
57 gbc.gridx=9;
58 gbc.gridwidth=1;
59 gbc.fill=GridBagConstraints.NONE;
60 gbl.setConstraints(sendButton,gbc);
61 mainp.add(sendButton);
62 gbc.gridx=0;
63 gbc.gridy=2;
64 gbc.gridwidth=10;
65 gbl.setConstraints(messageInfo,gbc);
66 mainp.add(messageInfo);
67 gbc.gridy=3;
68 gbc.weighty=100;
69 gbc.gridheight=10;
70 gbc.fill=GridBagConstraints.BOTH;
71 gbl.setConstraints(messageText,gbc);
72 mainp.add(messageText);
73 sendButton.addActionListener(this);
74 //構造的panel加入到applet中
75 setLayout(new BorderLayout());
76 add("Center",mainp);
77 chatURL=getCodeBase();
78 }
79 publicsynchronizedvoid start()
80 {
81 }
82 publicsynchronizedvoid stop()
83 {
84 }
85 publicsynchronizedvoid destroy()
86 {
87 }
88
89 //
90 privatevoid Send()
91 {
92 message=sendText.getText();
93 sendText.setText("");
94 //showStatus("Message send!");
95 p("Message send!");
96 messageText.setText("message");
97 String queryString="/servlet/Receive?message"+URLEncoder.encode(message);
98 p("Attemping to send :"+message);
99 try{
100 connect=(new URL(chatURL,queryString)).openConnection();
101 showStatus("Open Connection!");
102 connect.setDefaultUseCaches(false);
103 connect.setUseCaches(false);
104 connect.setDoInput(true);
105 connect.setDoOutput(false);
106 connect.connect();
107 p("Make connection to "+connect);
108 showStatus("Open Sream!");
109 DataInputStream in=new DataInputStream(connect.getInputStream());
110 showStatus("reading");
111 message=in.readLine();
112 while(message!=null)
113 {
114 messageText.setText(message);
115 message=in.readLine();
116
117 }//end while
118
119
120
121
122 }//end try
123 catch(MalformedURLException e2)
124 {
125 System.err.println("Malformedurlexception:"+e2.toString());
126
127 }//end catch
128 catch(IOException e1)
129 {
130 System.err.println(e1.toString());
131
132 }
133
134 }//end Send
135
136 publicvoid actionPerformed(ActionEvent e)
137 {
138 if(e.getSource()==sendButton)
139 {
140 Send();
141
142 }
143
144 }
145
146 privatevoid p(String debug)
147 {
148 messageText.setText(debug);
149 //System.out.println("app-servlet:"+debug);
150
151 }
152
153 }

  

posted on 2011-07-27 09:17  york_hust  阅读(853)  评论(0编辑  收藏  举报