JAVA经典代码(三)

23.编程实现序列化的Student(sno,sname)对象在网络上的传输
package com.softeem.demo;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;

class Student implements Serializable {
private int sno;
private String sname;

public Student(int sno, String sname) {
this.sno = sno;
this.sname = sname;
}

public int getSno() {
return sno;
}

public void setSno(int sno) {
this.sno = sno;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

@Override
public String toString() {
return "学号:" + sno + ";姓名:" + sname;
}

}

class MyClient extends Thread {
@Override
public void run() {
try {
Socket s = new Socket("localhost", 9999);
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Student stu = (Student) ois.readObject();
System.out.println("客户端程序收到服务器端程序传输过来的学生对象>> " + stu);
ois.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class MyServer extends Thread {

@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();
ObjectOutputStream ops = new ObjectOutputStream(s.getOutputStream());
Student stu = new Student(1, "赵本山");
ops.writeObject(stu);
ops.close();
s.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public class TestTransfer {
public static void main(String[] args) {
new MyServer().start();
new MyClient().start();
}
}
l JDBC
24.用dom4j组件解析如下XML格式的文件:
<?xml version="1.0" encoding="UTF-8"?>
<generator>
<table name="login" operation="1">
<column name="username" handle="0">aaa </column>
<column name="password" handle="0">123 </column>
</table>
<table name="login" operation="2">
<column name="id" handle="1">1 </column>
<column name="username" handle="0">bbb </column>
<column name="password" handle="0">444 </column>
</table>
<table name="login" operation="3">
<column name="id" handle="1">4 </column>
</table>
</generator>
规则: <table>operation 1表insert,2表update,3表delete.
<column>handle 1表作为where条件,0表作为操作字段。
要求:按照规则生成三条SQL语句!(即做一个方法解析xml文件生成一个含有三条SQL语句的字符串)

public String parseXmltoSQL(String xmlFileName) {
StringBuffer sbsql = new StringBuffer();
SAXReader reader = new SAXReader();
try {
Document document = reader.read(new File(xmlFileName));
Element element = document.getRootElement();
Iterator it = element.elementIterator("table");
while (it.hasNext()) {
element = (Element) it.next();
//获得对表的操作
String oper = element.attributeValue("operation");
//获得表名
String tableName = element.attributeValue("name");
if ("1".equals(oper)) {
sbsql.append("insert into ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
String columnName2 = null;
String columnValue2 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName2 = element.attributeValue("name");
columnValue2 = element.getText();
}
sbsql.append("("+columnName1+","+columnName2+")"+" values('"+columnValue1+"','"+columnValue2+"')\n");

} elseif ("2".equals(oper)) {
sbsql.append("update ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
String columnName2 = null;
String columnValue2 = null;
String columnName3 = null;
String columnValue3 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName2 = element.attributeValue("name");
columnValue2 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName3 = element.attributeValue("name");
columnValue3 = element.getText();
}
sbsql.append(" set "+columnName2+"='"+columnValue2+"',"+columnName3+"='"+columnValue3+"' where "+columnName1+"="+columnValue1+"\n");

}elseif ("3".equals(oper)) {
sbsql.append("delete from ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
sbsql.append(" where "+columnName1+"="+columnValue1);

}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sbsql.toString();
}
l JSP/SERVLET
25.写出JSP的内置对象并说明他们的作用
request:request表示HttpServletRequest对象。它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie, header数据的有用的方法。 response:response表示HttpServletResponse对象,并提供了几个用于设置送回浏览器的响应的方法(如cookies,头信息等) out:out对象是javax.jsp.JspWriter的一个实例,并提供了几个方法使你能用于向浏览器回送输出结果。 pageContext:pageContext表示一个javax.servlet.jsp.PageContext对象。它是用于方便存取各种范围的名字空间、servlet相关的对象的API,并且包装了通用的servlet相关功能的方法。 session:session表示一个请求的javax.servlet.http.HttpSession对象。Session可以存贮用户的状态信息 application:applicaton 表示一个javax.servle.ServletContext对象。这有助于查找有关servlet引擎和servlet环境的信息 config:config表示一个javax.servlet.ServletConfig对象。该对象用于存取servlet实例的初始化参数。 page:page表示从该页面产生的一个servlet实例。
exception: exception对象是一个例外对象,当一个页面在运行过程中发生了例外,就产生这个对象。如果一个JSP页面要应用此对象,就必须把isErrorPage设为true,否则无法编译。他实际上是java.lang.Throwable的对象

你可能感兴趣的:(java,jsp,servlet,socket,浏览器)