[b]一、Web应用中的几种属性:[/b]
(1)局部变量:doGet()或doPost()方法内定义的变量 线程安全
(2)实例变量:Servlet类体中定义的非静态成员变量(属性) 线程不安全--〉让Servlet类实现 SingleThreadModel 或加同步
(3)类变量:Servlet类体中定义的静态成员变量(属性) 线程不安全 --〉加同步
(4)请求属性:request.setAttribute() 线程安全
(5)会话属性:session.setAttribute() 线程不安全 --〉加同步
(6)上下文属性:ctx.setAttribute() 线程不安全 --〉加同步
[b]二、web应用异常处理(定制错误页面):[/b]
(1)http错误:
在web.xml中
(2)Java异常:两种处理方式:
a) 在web.xml中
b) 编程:
在原始的Servlet中写
(1)局部变量:doGet()或doPost()方法内定义的变量 线程安全
(2)实例变量:Servlet类体中定义的非静态成员变量(属性) 线程不安全--〉让Servlet类实现 SingleThreadModel 或加同步
(3)类变量:Servlet类体中定义的静态成员变量(属性) 线程不安全 --〉加同步
(4)请求属性:request.setAttribute() 线程安全
(5)会话属性:session.setAttribute() 线程不安全 --〉加同步
(6)上下文属性:ctx.setAttribute() 线程不安全 --〉加同步
[b]二、web应用异常处理(定制错误页面):[/b]
(1)http错误:
在web.xml中
<error-page>
<error-code>某个http错误号,例如404</error-code>
<location>转发的目的页面URL(.html或Servlet)</location>
</error-page>
(2)Java异常:两种处理方式:
a) 在web.xml中
<error-page>
<exception-type>Java异常的完整类名</exception-type>
<location>转发的目的页面URL(.html或Servlet)/location>
</error-page>
b) 编程:
在原始的Servlet中写
try{
//可能抛出异常的语句
}catch(XXXException e){
ServletContext ctx = getServletContext();
RequestDispatcher rd = ctx.getNamedDispatcher("目的Servlet的名字");
rd.forward(request, response);
}