1、序列化与反序列化必须定义serialVersionUID ,否则读取之前序列化的对象,如果改动属性则java.io.InvalidClassException: com.yza.Student; local class incompat
private static final long serialVersionUID = 1L;
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(new Student());
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Student s1 = (Student) ois.readObject();
2、类中避免使用公共常量,定义私有再提供get方法,否则容易出现子类覆盖父类属性
3、登录后进入之前的界面,直接保存在request中,跳转可以<meta http-equiv="refresh" content="3,url=index.jsp"/>
4、tomcat下单例不唯一,每个项目都会有各自的实例对象。
5、servlet线程不安全,故不能并发地写属性,但可以读取属性。
public class TestServlet extends HttpServlet {
private String name;//定义一个公共的私有变量
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("GB2312");
name=request.getParameter("name");//获取name参数
try {
Thread.sleep(10000);//让程序沉睡10秒钟
} catch (InterruptedException e) {
e.printStackTrace();
}
response.getWriter().println("获取的name参数为:"+name);//输出信息
}
}
6、if语句捕捉转换异常
Object obj;// 定义Object对象
Date dt = new Date();// 定义日期对象
obj = new Object();
//利用if语句判断对象的数据类型是否相同
if(obj.getClass().getSimpleName().equals(dt.getClass().getSimpleName())){
dt = (Date) obj;// 将object对象强制转换为
}else{
System.out.println("obj:"+obj.getClass().getSimpleName());
System.out.println("dt:"+dt.getClass().getSimpleName());
System.out.println("数据类型转换失败");
}
7、数组存储速度高于hashmap,但查询相反
8、区分DOM属性(文档中的节点)与元素属性(指的是html中id、value等属性)
9、数据库能实现的业务推荐用数据库实现。
10、javascript有预解析功能,且同名变量或方法会被后面的覆盖。
11、include指令包含进来再一起编译,故不能相同变量,而jsp:include会编译执行后再添加进来
12、注意非空验证
List<StuGraduationCustomPassVo> voList = null;
List list = this.stuPassDetailDao.findStuGraduationCustomPassVoListByCondition(map);
if (list != null && list.size() > 0) {
voList = new ArrayList<StuGraduationCustomPassVo>();
for (Object object : list) {
Object[] obj = (Object[]) object;
StuGraduationCustomPassVo vo = new StuGraduationCustomPassVo();
...
voList.add(vo);
}
}
return voList;
这种情况for循环或者foreach必须非空验证,否则报空指针
List list = this.stuPassDetailDao.findStuPublishDetailVoList(stuTeaId,i);
if (list != null && list.size() > 0) {
for (Object object : list) {