一个小程序得逻辑问题,如何解决?在线等待,立刻给分

pingju 2002-04-26 09:41:23
想做一个通过applet聊天的web小程序,服务器端就一个servlet负责收集applet中的文本框内容然后显示出来。想将内容放置到servletcontext对象中让大家都能看见,类似与asp中application 变量做的聊天室。可我做的东西,只能自己发的自己看见,别人看不见,看看我的程序,怎么改?

servlet文件:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class GetServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
/**Initialize global variables*/
public void init() throws ServletException {
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintStream out=new PrintStream(response.getOutputStream());
ServletContext sc=getServletContext();
String s=request.getParameter("name");
String s1="";

if(sc.getAttribute("talk")==null)
{
s1="";
}
else
{
s1=s1+request.getParameter("name");
}
sc.setAttribute("talk",s1);
String s2=(String)sc.getAttribute("talk");
out.println(s2);
out.close();
}
/**Clean up resources*/
public void destroy() {
}
}
***我知道问题出现在变量s1上,他只保存着每个人的信息,而不是全局的。应怎么样改??

applet文件:


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class GetApplet extends Applet
{

Panel panel1=new Panel();
Label label1=new Label();
TextField nameFd=new TextField();
BorderLayout borderlayout1=new BorderLayout();
Button sendBtn=new Button();
TextArea result=new TextArea();



/**Construct the applet*/
public GetApplet() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception
{
this.setSize(300,160);
label1.setText("姓名");
this.setLayout(borderlayout1);
nameFd.setColumns(20);
sendBtn.setLabel("发送");
sendBtn.addActionListener(new B1());



this.add(panel1,BorderLayout.NORTH);
panel1.add(label1,null);
panel1.add(nameFd,null);
panel1.add(sendBtn,null);
this.add(result,BorderLayout.CENTER);

}

void sendBtn_actionPerformed(ActionEvent e)
{
String protocol=getCodeBase().getProtocol();
int port=getCodeBase().getPort();
String host=getCodeBase().getHost();
try
{

URL url= new URL(protocol + "://" + host + ":" + port + "/test/servlet/GetServlet?name=" + URLEncoder.encode(nameFd.getText()));

DataInputStream reader=new DataInputStream(url.openStream());
result.append("echo:姓名=" + reader.readLine() + "\r\n");
}
catch(MalformedURLException mle)
{
}
catch(IOException ioe)
{
}


}
class B1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//getAppletContext().showStatus("Button 1");

String protocol=getCodeBase().getProtocol();
int port=getCodeBase().getPort();
String host=getCodeBase().getHost();
try
{

URL url= new URL(protocol + "://" + host + ":" + port + "/test/servlet/GetServlet?name=" + URLEncoder.encode(nameFd.getText()));

DataInputStream reader=new DataInputStream(url.openStream());
result.append("echo:姓名=" + reader.readLine() + "\r\n");
}
catch(MalformedURLException mle)
{
}
catch(IOException ioe)
{
}


}
}

/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
}
...全文
67 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pingju 2002-04-27
  • 打赏
  • 举报
回复
关注!!!!
pingju 2002-04-27
  • 打赏
  • 举报
回复
gz!
pingju 2002-04-26
  • 打赏
  • 举报
回复
to dylanwolf:
你说的什么呀?怎么改的?将DataInputStream reader改为BufferedReaderam reader,那成什么了,具体点,哥们
dylanwolf 2002-04-26
  • 打赏
  • 举报
回复
ServeltContext就相当于jsp 的 application
dylanwolf 2002-04-26
  • 打赏
  • 举报
回复

DataInputStream reader=new DataInputStream(url.openStream());
result.append("echo:姓名=" + reader.readLine


将DataInputStream reader改为BufferedReader
pingju 2002-04-26
  • 打赏
  • 举报
回复
servlet里面有application对象吗?不是jsp?
tin_dra 2002-04-26
  • 打赏
  • 举报
回复
将内容放置到application对象
application.getAttribute("talk");
application.setAttribute("talk",s1);

81,092

社区成员

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

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