您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 基于BS结构的applet和servlet通信实例
基于B/S架构的applet和servlet通信Applet的优缺点这里也不想细说,百度上面多的是,只不过实例多为一个版本,这几天也研究了一下,写了个小例子,希望可以对大家有用.1.首先页面如下,主要完成的是增删改查的操作,数据传递主要依靠的是对象的序列化,封装了两个对象TempObject(客户到服务端)和ServerObject(服务到客户端),页面中的刷新是全表格的刷新,增删改操作后有局部动态刷新,整体布局为GridBagLayout和BorderLayout相结合,包的划分如下:2.代码部分Client端主要是ManageInfo,java:代码如下:packagecom.zcsoft.frame;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.io.IOException;importjava.io.InputStream;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.OutputStream;importjava.net.MalformedURLException;importjava.net.URL;importjava.net.URLConnection;importjava.util.Calendar;importjava.util.List;importjava.util.Vector;importjavax.print.attribute.standard.Severity;importjavax.swing.JApplet;importjavax.swing.JButton;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.JTextField;importjavax.swing.UIManager;importjavax.swing.UnsupportedLookAndFeelException;importjavax.swing.border.LineBorder;importjavax.swing.event.ListSelectionEvent;importjavax.swing.event.ListSelectionListener;importjavax.swing.table.DefaultTableModel;importjavax.swing.table.TableColumn;importjavax.swing.table.TableColumnModel;importsun.security.krb5.internal.UDPClient;importcom.sun.corba.se.impl.ior.WireObjectKeyTemplate;importcom.zcsoft.dao.EmployeeDao;importcom.zcsoft.test.Employee;importcom.zcsoft.test.ServerObject;importcom.zcsoft.test.TempObject;/***页面窗体*@authortraining**/publicclassManageInfoextendsJApplet{privateJTabletab;privateDefaultTableModeltabModel;//文本框privateJTextFieldtxtName,txtBirth,txtPhone,txtEmail,txtAddress;//下拉列表privateJComboBoxcboSex,cboEducation;//按钮privateJButtonbtnUpd,btnAdd,btnMod,btnDel;privateURLurl;privateURLConnectionurlcon;privateInputStreamins=null;privateObjectInputStreamois=null;privateObjectOutputStreamoos=null;privateObjectresult=null;privateCalendarcalendar;publicManageInfo(){setBounds(200,200,900,700);setLayout(newBorderLayout());//组件初始化componentInit();//校验服务端if(checkServer()){//数据初始化InitData();}//绑定监听器bindListener();//cboEducation.addActionListener()/*tabModel.addRow(rowData);tabModel.setValueAt(aValue,row,column);tabModel.removeRow();*///tab.getSelectionModel().addListSelectionListener(newListSelectionListener(){//@Override//publicvoidvalueChanged(ListSelectionEvente){////}//});}/***组件初始化*/privatevoidcomponentInit(){//用于BorderLayout布局的上中下面板JPanelpnlTop,pnlFoot;JScrollPanespnlTab;//实例化文本框txtName=newJTextField(10);txtBirth=newJTextField(10);txtPhone=newJTextField(10);txtEmail=newJTextField(10);txtAddress=newJTextField(40);//实例化下拉列表cboEducation=newJComboBox(newString[]{博士,硕士,本科,专科,高中});cboSex=newJComboBox(newString[]{男,女});//实例化按钮btnUpd=newJButton(刷新);btnAdd=newJButton(添加);btnDel=newJButton(删除);btnMod=newJButton(修改);//主体部分pnlTop=newJPanel(newGridBagLayout());pnlTop.setBorder(newLineBorder(Color.red,1));//底部pnlFoot=newJPanel();pnlFoot.add(btnUpd);pnlFoot.add(btnAdd);pnlFoot.add(btnMod);pnlFoot.add(btnDel);//实例化表格String[]columns=newString[]{编号,姓名,出生年月,性别,文化程度,联系电话,电子邮件,通讯地址};Object[][]data=null;tabModel=newDefaultTableModel(data,columns);tab=newJTable(tabModel);//禁止拖动tab.getTableHeader().setReorderingAllowed(false);//实例化滚动面板,将表格加入其中spnlTab=newJScrollPane(tab);//将组件添加至pnlTop中GridBagConstraintsgbc=newGridBagConstraints();gbc.insets=newInsets(10,10,10,20);gbc.gridy=0;gbc.anchor=GridBagConstraints.NORTHWEST;pnlTop.add(newJLabel(姓名),gbc);pnlTop.add(txtName,gbc);pnlTop.add(newJLabel(出生日期),gbc);pnlTop.add(txtBirth,gbc);pnlTop.add(newJLabel(性别),gbc);pnlTop.add(cboSex,gbc);gbc.gridy=1;pnlTop.add(newJLabel(文化程度),gbc);pnlTop.add(cboEducation,gbc);pnlTop.add(newJLabel(联系电话),gbc);pnlTop.add(txtPhone,gbc);pnlTop.add(newJLabel(电子邮件),gbc);pnlTop.add(txtEmail,gbc);gbc.gridy=2;pnlTop.add(newJLabel(通讯地址),gbc);gbc.gridwidth=5;pnlTop.add(txtAddress,gbc);gbc.gridy=3;gbc.gridwidth=6;gbc.fill=GridBagConstraints.HORIZONTAL;this.add(pnlTop,BorderLayout.NORTH);this.add(spnlTab,BorderLayout.CENTER);this.add(pnlFoot,BorderLayout.SOUTH);}/***绑定监听器*/privatevoidbindListener(){btnUpd.addActionListener(newbtnClick());btnAdd.addActionListener(newbtnClick());btnDel.addActionListener(newbtnClick());btnMod.addActionListener(newbtnClick());tab.getSelectionModel().addListSelectionListener(newtabSelectedChange());}/***表格变更行监听实现类*@authortraining**/privateclasstabSelectedChangeimplementsListSelectionListener{@OverridepublicvoidvalueChanged(ListSelectionEvente){//获取选中的行的对象if(tab.getSelectedRow()==-1){return;}if(!e.getValueIsAdjusting()){//清空组件InitFrame();intsel=tab.getSelectedRow();if(tab.getValueAt(sel,1)!=null){txtName.setText(tab.getValueAt(sel,1).toString());}if(tab.getValueAt(sel,2)!=null){txtBirth.setText(tab.getValueAt(sel,2).toString());}if(tab.getValueAt(sel,5)!=null){txtPhone.setText(tab.getValueAt(sel,5).toString());}if(tab.getValueAt(sel,6)!=null){txtEmail.
本文标题:基于BS结构的applet和servlet通信实例
链接地址:https://www.777doc.com/doc-315207 .html