java web -- jsp 和 servlet 线程安全
来源:互联网 发布:如何获得淘宝客户信息 编辑:程序博客网 时间:2024/05/29 04:59
最近看了一些JAVA WEB开发部分,发现JSP和Servlet需要注意线程安全,以前玩过PHP,知道犹豫PHP的运行方式,所以PHP每个页面每次访问都是独立的,所以不用考虑线程安全问题。突然间要在WEB开发中需要注意线程安全所以有些摸不着头脑,于是也从JSP和Servlet运行方式入手,从而彻底把握JAVA WEB开发线程安全问题!
容器对于Servlet 只创建一个实例,对于每次请求都使用单独一个线程来执行,这样可提高效率,也就是每次都是多线程的执行service()函数,因此可知,对于Servlet的成员变量和静态成员变量对于所有线程是共享的,所谓共享数据就是需要注意线程安全的,而在service()函数的生命的所有变量都是局部的,每次执行都是独立的。还有就是Application变量也需要注意!
对于JSP也是如此道理,因为JSP会被翻译成Servlet,只要搞清楚JSP那些部分被翻译为成员变量了!
JSP当中可以使用<% %>和<%! %>,只要搞清楚这两部分的代码被翻译为什么!
test.jsp
<%...@ page language="java" import="java.util.*" pageEncoding="gb2312"
isThreadSafe="true" %>
<%...!
String str1;
%>
<%...
String str2;
%>
isThreadSafe="true" %>
<%...!
String str1;
%>
<%...
String str2;
%>
可以看到test.jsp被翻译成的servlet
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent ...{
String str1;
private static java.util.List _jspx_dependants;
public Object getDependants() ...{
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException ...{
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try ...{
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=gb2312");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write(' ');
out.write(' ');
out.write(' ');
out.write(' ');
String str2;
} catch (Throwable t) ...{
if (!(t instanceof SkipPageException))...{
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally ...{
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent ...{
String str1;
private static java.util.List _jspx_dependants;
public Object getDependants() ...{
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException ...{
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try ...{
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=gb2312");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write(' ');
out.write(' ');
out.write(' ');
out.write(' ');
String str2;
} catch (Throwable t) ...{
if (!(t instanceof SkipPageException))...{
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally ...{
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
str被翻译为成员变量, str2被翻译为局部变量!
不用多说就明白了!
避免线程安全的方法:
jsp: <%@ isThreadSafe="false" %> 这样容器就以单线程的方式执行,不过仍然只有一个实例!成员变量还是共享的!
servlet: 声明service()函数为synchronized ,性能降低!
- java web -- jsp 和 servlet 线程安全
- JSP/Servlet线程安全
- Java Web-w2:JSP和SERVLET区别
- java Web(Jsp和Servlet登录校验)
- Servlet、jsp的生命周期,线程安全,感悟
- 过滤器,监听器,Servlet 线程安全,JSP基础
- Servlet的多线程 和 Servlet线程安全
- Servlet的多线程 和 Servlet线程安全
- Servlet 多线程和Servlet线程安全
- Servlet的多线程 和 Servlet线程安全
- java web与servlet jsp
- JSP和SERVLET+WEB.XML
- java web开发中,如何查看JSP和Servlet版本
- java web开发中,如何查看JSP和Servlet版本
- java web开发中,如何查看JSP和Servlet版本
- java web开发中,如何查看JSP和Servlet版本
- java web开发中,如何查看JSP和Servlet版本
- java web开发中,如何查看JSP和Servlet版本
- Visual Studio 2003 中automation服务器不能创建对象
- 只允许输入数字的一个小方法
- 数据结构——基本数据结构之队列
- 5分钟,流量加倍!
- 你不能不知道的经典问题
- java web -- jsp 和 servlet 线程安全
- 二重啟動check
- 控制JS的加载
- 【转】MS-SQL手册
- 你不能不知道的人生10个秘密
- 越过调试这道槛--ASP.net无法调试问题剖析
- jsp页面转向(跳转)
- 体会Bind和Eval的不同用法
- IE7下动态创建Iframe时,去除边框