bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

java網頁聊天代碼,javaweb在線聊天系統

急需一個java編程實現的簡單聊天窗口代碼

import java.awt.*;

“只有客戶發展了,才有我們的生存與發展!”這是成都創新互聯的服務宗旨!把網站當作互聯網產品,產品思維更注重全局思維、需求分析和迭代思維,在網站建設中就是為了建設一個不僅審美在線,而且實用性極高的網站。創新互聯對成都網站設計、成都網站建設、網站制作、網站開發、網頁設計、網站優化、網絡推廣、探索永無止境。

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ClientDemo01 {

public static void main(String[] args){

JFrame f=new JFrame("AA");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JTextArea ta=new JTextArea(15,30);

ta.setEditable(false); //文本域只讀

JScrollPane sp=new JScrollPane(ta); //滾動窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("發送");

p1.add(sp);

p2.add(tf);

p2.add(b);

f.add(p1,"Center");

f.add(p2,"South");

f.setBounds(300,300,360,300);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Socket socket=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try{

socket=new Socket("192.168.0.4",5000);

bis=new BufferedInputStream(socket.getInputStream());

bos=new BufferedOutputStream(socket.getOutputStream());

MyThread01 mt=new MyThread01(bis,ta);

mt.start();

}catch(Exception e){

e.printStackTrace();

}

b.addActionListener(new ButtonActionListener01(tf,ta,bos));

}

}

class ButtonActionListener01 implements ActionListener{

JTextField tf;

JTextArea ta;

BufferedOutputStream bos;

public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf;

this.ta=ta;

this.bos=bos;

}

public void actionPerformed(ActionEvent e){

String message=tf.getText();

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("AA:"+message+"\n"); //添加到文本域并換行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("發送失敗");

}

}

}

}

class MyThread01 extends Thread{

BufferedInputStream bis;

JTextArea ta;

public MyThread01(BufferedInputStream bis,JTextArea ta){

this.bis=bis;

this.ta=ta;

}

public void run(){

try{

while(true){

byte[] b=new byte[100];

int length=bis.read(b);

String message=new String(b,0,length);

ta.append("BB:"+message+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

} import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ServerDemo01{

public static void main(String[] args){

JFrame f=new JFrame("BB");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JTextArea ta=new JTextArea(12,30); //文本域,第一個參數為行數,第二個參數為列數

ta.setEditable(false); //文本域只讀

JScrollPane sp=new JScrollPane(ta); //滾動窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("發送");

p1.add(sp);

p2.add(tf);

p2.add(b);

f.add(p1,"Center");

f.add(p2,"South");

f.setBounds(300,300,360,300);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ServerSocket server=null;

Socket socket=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try{

server=new ServerSocket(5000);

//ta.append("等待AA連接...\n");

socket=server.accept();

//ta.append("AA已連接\n");

bis=new BufferedInputStream(socket.getInputStream());

bos=new BufferedOutputStream(socket.getOutputStream());

MyThread1 mt=new MyThread1(bis,ta);

mt.start();

}catch(Exception e){

e.printStackTrace();

}

b.addActionListener(new ButtonActionListener1(tf,ta,bos));

}

}

class ButtonActionListener1 implements ActionListener{

JTextField tf;

JTextArea ta;

BufferedOutputStream bos;

public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf;

this.ta=ta;

this.bos=bos;

}

public void actionPerformed(ActionEvent e){

String message=tf.getText(); //獲取文本框中的內容

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("BB:"+message+"\n"); //添加到文本域并換行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("發送失敗!");

}

}

}

}

class MyThread1 extends Thread{

BufferedInputStream bis;

JTextArea ta;

public MyThread1(BufferedInputStream bis,JTextArea ta){

this.bis=bis;

this.ta=ta;

}

public void run(){

try{

while(true){

byte[] b=new byte[100];

int length=bis.read(b);

String message=new String(b,0,length);

ta.append("AA:"+message+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

}

用JAVA 編寫簡單網絡聊天程序

/**

* 基于UDP協議的聊天程序

*

* 2007.9.18

* */

//導入包

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.net.*;

public class Chat extends JFrame implements ActionListener

{

//廣播地址或者對方的地址

public static final String sendIP = "172.18.8.255";

//發送端口9527

public static final int sendPort = 9527;

JPanel p = new JPanel();

List lst = new List(); //消息顯示

JTextField txtIP = new JTextField(18); //填寫IP地址

JTextField txtMSG = new JTextField(20); //填寫發送消息

JLabel lblIP = new JLabel("IP地址:");

JLabel lblMSG = new JLabel("消息:");

JButton btnSend = new JButton("發送");

byte [] buf;

//定義DatagramSocket的對象必須進行異常處理

//發送和接收數據報包的套接字

DatagramSocket ds = null;

//=============構造函數=====================

public Chat()

{

CreateInterFace();

//注冊消息框監聽器

txtMSG.addActionListener(this);

btnSend.addActionListener(this);

try

{

//端口:9527

ds =new DatagramSocket(sendPort);

}

catch(Exception ex)

{

ex.printStackTrace();

}

//============接受消息============

//匿名類

new Thread(new Runnable()

{

public void run()

{

byte buf[] = new byte[1024];

//表示接受數據報包

while(true)

{

try

{

DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort);

ds.receive(dp);

lst.add("【消息來自】◆" + dp.getAddress().getHostAddress() + "◆"+"【說】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0);

}

catch(Exception e)

{

if(ds.isClosed())

{

e.printStackTrace();

}

}

}

}

}).start();

//關閉窗體事件

this.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent w)

{

System.out.println("test");

int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION);

if(n==JOptionPane.YES_OPTION)

{

dispose();

System.exit(0);

ds.close();//關閉ds對象//關閉數據報套接字

}

}

});

}

//界面設計布局

public void CreateInterFace()

{

this.add(lst,BorderLayout.CENTER);

this.add(p,BorderLayout.SOUTH);

p.add(lblIP);

p.add(txtIP);

p.add(lblMSG);

p.add(txtMSG);

p.add(btnSend);

txtIP.setText(sendIP);

//背景顏色

lst.setBackground(Color.yellow);

//JAVA默認風格

this.setUndecorated(true);

this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

this.setSize(600,500);

this.setTitle("〓聊天室〓");

this.setResizable(false);//不能改變窗體大小

this.setLocationRelativeTo(null);//窗體居中

this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

this.setVisible(true);

txtMSG.requestFocus();//消息框得到焦點

}

//===============================Main函數===============================

public static void main(String[]args)

{

new Chat();

}

//================================發送消息===============================

//消息框回車發送消息事件

public void actionPerformed(ActionEvent e)

{

//得到文本內容

buf = txtMSG.getText().getBytes();

//判斷消息框是否為空

if (txtMSG.getText().length()==0)

{

JOptionPane.showMessageDialog(null,"發送消息不能為空","提示",JOptionPane.WARNING_MESSAGE);

}

else{

try

{

InetAddress address = InetAddress.getByName(sendIP);

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);

ds.send(dp);

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

txtMSG.setText("");//清空消息框

//點發送按鈕發送消息事件

if(e.getSource()==btnSend)

{

buf = txtMSG.getText().getBytes();

try

{

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);

}

catch(Exception ex)

{

ex.printStackTrace();

}

txtMSG.setText("");//清空消息框

txtMSG.requestFocus();

}

}

}

jsp網頁怎么實現即時聊天

jsp中可以實現簡單的聊天功能,例子如下:

chat.jsp代碼如下:

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%

%@ page language="java" contentType="text/html;charset=GB2312"%

html

body

%

try{

request.setCharacterEncoding("GB2312");

String mywords=request.getParameter("message");

String t="";

if(application.getAttribute("words")==null mywords!=null){

t= (String)request.getRemoteAddr() + ":" + mywords + "br/";

application.setAttribute("words",(Object)t);

out.println(t);

}

else if(mywords!=null){

t=(String)application.getAttribute("words");

t += (String)request.getRemoteAddr() + ":" + mywords + "br/";

application.setAttribute("words",(Object)t);

out.println(t);

}

}

catch(Exception e){

}

%

form method="post" action="index.jsp"

input name="message" type="text" size=50

input type="submit" value="發送消息"

/form

/body/html

輸出對話內容如下:

Java網頁聊天如何實現,問題如下:

改成下面的樣子:

%@ page language="java" pageEncoding="GBK"%

html

head

titleMy JSP 'lab.jsp' starting page/title

/head

body

%

if(application.getAttribute("chat")!=null)

{ if(request.getParameter("mywords")!=null)

{ String mywords=request.getParameter("mywords");

mywords=(String)application.getAttribute("chat")+"br"+mywords;

application.setAttribute("chat",mywords);

out.print((String)application.getAttribute("chat"));

}

}else{

application.setAttribute("chat","");

}

%

form action="a1.jsf" method="post"

input type="text" size="30" name="mywords" value="我喜歡聊天"

input type="submit" name="sumbit" value="提交"

/form

/body

/html

1.要用post提交,否則中文亂碼

2.out.print((String)application.getAttribute("char")); 這句你寫錯了,應該是chat不是char

3.你第一次運行的時候application.getAttribute("chat")肯定是空的,是空的話你又什么都不作,下次還是空的呀

本文名稱:java網頁聊天代碼,javaweb在線聊天系統
網頁地址:http://vcdvsql.cn/article40/hsojeo.html

成都網站建設公司_創新互聯,為您提供外貿網站建設做網站網站策劃自適應網站網站收錄軟件開發

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站優化排名