当jsp+javabean转换为servlet时,servlet中一些代码的问题。
package abc; //这是测试用的简单javabean: abc.jsptest.java
public class jsptest{
public jsptest()
{
}
private String abc=new String("1234567") ;
public String getabc()
{
return this.abc;
}
}
<%@ page contentType="text/html; charset=gb2312" %>//这是jsptest.jsp文件
<jsp:useBean id="validate" scope="session" class="abc.jsptest" />
<html>
<head></head>
<body>this is the result
<%=validate.getabc()%>
</body>
</html>
这是转化成servlet后的代码:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class jsptest_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static java.util.Vector _jspx_dependants;
public java.util.List 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('\r');
out.write('\n');
abc.jsptest validate = null;
synchronized (session) {//?????????????
validate = (abc.jsptest) _jspx_page_context.getAttribute("validate", PageContext.SESSION_SCOPE);
if (validate == null){
validate = new abc.jsptest();
_jspx_page_context.setAttribute("validate", validate, PageContext.SESSION_SCOPE);
}
}
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head></head>\r\n");
out.write("<body>this is the result\r\n");
out.write("\r\n");
out.print(validate.getabc());
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("</body>\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("</html>");
} 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);
}
}
}
请高人们解释下注释问号处的代码及对象作用,以及为什么要在 synchronized (session)处用session对象同步代码段保证线程安全呢?这是为了防止何种情况下的不安全因素发生?可否举个简单例子呢?