向servlet发送流数据,失败,请大虾们帮我看看,ok?

DrunkenLion 2001-10-16 10:19:02
///==这是applet程序
package untitled7;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import com.borland.jbcl.layout.*;
public class Applet1 extends Applet
{
boolean isStandalone = false;

URLConnection connect;
//用于保存用户的输入信息
String message;
URL ChatURL;
GridLayout gridLayout1 = new GridLayout();
JPanel jPanel3 = new JPanel();
JTextArea messageText = new JTextArea();
XYLayout xYLayout2 = new XYLayout();
JPanel jPanel4 = new JPanel();
JPanel jPanel2 = new JPanel();
JButton sendButton = new JButton();
JTextField sendText = new JTextField();
//返回一个描述本applet的字符串

public String getAppletInfo()
{
return "sendApplet - Applet to communicate with receiveServlet,hehe";
}
/**Construct the applet*/

/**Initialize the applet*/
public void init()
{
super.init();
this.setSize(new Dimension(400,300));
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
// ChatURL = getCodeBase();
}
public synchronized void start()
{

}
public synchronized void stop()
{

}
public synchronized void destroy()
{

}
private void Send()
{
message = sendText.getText();
sendText.setText("");
String queryString = "servlet/untitled7.Servlet1?message="+URLEncoder.encode(message);
try
{
connect = (new URL(ChatURL,queryString).openConnection());
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.connect();
DataOutputStream aa = new DataOutputStream(connect.getOutputStream());
aa.writeInt(123);
aa.writeUTF("中国");
aa.writeLong(12);
aa.writeUTF("靠,去司把");
aa.flush();
// aa.close();
/* DataInputStream in = new DataInputStream(connect.getInputStream());
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!");
e1.printStackTrace(System.err);
showStatus("IOException!");
}
}
private void jbInit() throws Exception
{
this.setLayout(gridLayout1);
try
{
ChatURL=new URL("http://localhost:8080/");
}
catch(java.net.MalformedURLException e)
{

}
messageText.setText("jTextArea1");
jPanel3.setLayout(xYLayout2);
sendButton.setText("Send");
sendButton.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
sendButton_mouseClicked(e);
}
});
sendButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sendButton_actionPerformed(e);
}
});
sendText.setText("jTextField1");
this.add(jPanel3, null);
jPanel3.add(jPanel2, new XYConstraints(0, 7, 400, 75));
jPanel2.add(sendText, null);
jPanel2.add(jPanel4, null);
jPanel4.add(sendButton, null);
jPanel3.add(messageText, new XYConstraints(63, 136, 274, 129));
}

void sendButton_actionPerformed(ActionEvent e)
{

this.Send();
}

void sendButton_mouseClicked(MouseEvent e)
{

}
}

//====这是servlet程序
package untitled7;


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
* Title: 呵呵
* Description: 测试而已
* Copyright: Copyright (c) 2001
* Company: worksoft
* @author
* @version 1.0
*/

public class Servlet1 extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html";
/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
/**Clean up resources*/
public void destroy()
{
}
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
System.out.println("communication ok");
//res.setContentType("text/plain");
String msg = req.getParameter("message");
if(msg==null)
return;
DataInputStream inn = new DataInputStream(req.getInputStream());
int aa = inn.readInt();
System.out.println(aa);
String bb = inn.readUTF();
System.out.println(bb);
System.out.println(inn.readLong());
System.out.println(inn.readUTF());
inn.close();
/** ServletOutputStream out = res.getOutputStream();
out.print("Receive user message");
out.println(req.getParameter("message"));
out.flush();
out.close();
*/
}
}

...全文
83 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lliang2 2001-10-16
  • 打赏
  • 举报
回复
你没写具体的出错信息,我只有猜了,
估计是你同时发了url参数和stream数据,
你将他们统统用stream发送试试!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧