当jsp+javabean转换为servlet时,servlet中一些代码的问题。

cloudlast 2006-05-06 03:22:18
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对象同步代码段保证线程安全呢?这是为了防止何种情况下的不安全因素发生?可否举个简单例子呢?
...全文
117 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧