[img]http://hfhwan.iteye.com/upload/attachment/102513/17299074-3709-3d58-a92c-cbca4edfa965.png[/img]
[b]ServletContext接口[/b]
是服务器中的一个一直路径的根。有容器厂商提供实现。一个程序只有一个,可被所有servlet访问。
请求转发。 sendRedirect和forward的区别
[b]servlet 异常处理[/b]
1. 声明式异常
2. 程序式异常
[b]声明式异常:[/b]
<error-page>
<error-code>404</error-code>
<location>/FileNotFound.html</location>
</error-page>
其中“404”“501”等是http协议规定的。
<error-page>
<exception-type>java.io.fileNotFoundException</eexception-type>
<location>/FileNotFound.html</location>
</error-page>
确定是:对于每个可能出现的java异常都要定义处理页面或者处理servlet
[b]程序式异常[/b]
用 try...catch...finally...用日志记录,并返回错误页面。
用RequestDispather来转发异常到统一的servlet来处理。
例如:
request.setAttribute("javax.servlet.error.exception", e)
[b]
servlet 线程安全[/b]
一个servlet实例在多个线程中运行。
1.变量的线程安全: 本地变量总是安全的。实例变量(?)和类变量不安全。
2.属性的现场安全: 请求对象(request)的是属性是安全的。Session和ServletContext 对象属性是不安全的。
建议:
*尽量使用本地变量
*只读的实例变量和静态变量
*servlet中不要创建线程了
*如果修改共享对象,要同步(sychronized),尽量缩小同步代码的范围,以免影响性能。
*在不同的servlet中,要访问外部对象(e.g.文件)进行修改操作,要加锁,互斥访问。
/*
*.do (不能有斜杠)
/action/*.do (不可以)
[b]ServletContext接口[/b]
是服务器中的一个一直路径的根。有容器厂商提供实现。一个程序只有一个,可被所有servlet访问。
请求转发。 sendRedirect和forward的区别
[b]servlet 异常处理[/b]
1. 声明式异常
2. 程序式异常
[b]声明式异常:[/b]
<error-page>
<error-code>404</error-code>
<location>/FileNotFound.html</location>
</error-page>
其中“404”“501”等是http协议规定的。
<error-page>
<exception-type>java.io.fileNotFoundException</eexception-type>
<location>/FileNotFound.html</location>
</error-page>
确定是:对于每个可能出现的java异常都要定义处理页面或者处理servlet
[b]程序式异常[/b]
用 try...catch...finally...用日志记录,并返回错误页面。
用RequestDispather来转发异常到统一的servlet来处理。
例如:
request.setAttribute("javax.servlet.error.exception", e)
[b]
servlet 线程安全[/b]
一个servlet实例在多个线程中运行。
1.变量的线程安全: 本地变量总是安全的。实例变量(?)和类变量不安全。
2.属性的现场安全: 请求对象(request)的是属性是安全的。Session和ServletContext 对象属性是不安全的。
建议:
*尽量使用本地变量
*只读的实例变量和静态变量
*servlet中不要创建线程了
*如果修改共享对象,要同步(sychronized),尽量缩小同步代码的范围,以免影响性能。
*在不同的servlet中,要访问外部对象(e.g.文件)进行修改操作,要加锁,互斥访问。
/*
*.do (不能有斜杠)
/action/*.do (不可以)