最近看了一些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"
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
isThreadSafe="true"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
!
String str1;
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
String str2;
%>
可以看到test.jsp被翻译成的servlet
package
org.apache.jsp;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.servlet.
*
;
import
javax.servlet.http.
*
;
import
javax.servlet.jsp.
*
;
import
java.util.
*
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
final
class
test_jsp
extends
org.apache.jasper.runtime.HttpJspBase
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
implements
org.apache.jasper.runtime.JspSourceDependent
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
String str1;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static java.util.List _jspx_dependants;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Object getDependants() ...{
return _jspx_dependants;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public void _jspService(HttpServletRequest request, HttpServletResponse response)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
throws java.io.IOException, ServletException ...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
out.write(' ');
out.write(' ');
out.write(' ');
out.write(' ');
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
String str2;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Throwable t) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} finally ...{
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
str被翻译为成员变量, str2被翻译为局部变量!
不用多说就明白了!
避免线程安全的方法:
jsp: <%@ isThreadSafe="false" %> 这样容器就以单线程的方式执行,不过仍然只有一个实例!成员变量还是共享的!
servlet: 声明service()函数为synchronized ,性能降低!