2014-10-08 33 views
0

之间有什么区别的同异:这两方法在jsp页面

this.log("log message"); 

((HttpServlet)page).log("anothermessage"); 

+3

什么'this'代表JSP?你在使用** Scriplet **吗? – Braj 2014-10-08 13:12:28

+0

'JspServlet'扩展'HttpServlet'。所以没有区别。 'GenericServlet'中定义了'log()'方法,它们都是超类。 – Braj 2014-10-08 13:14:57

+0

是的,我使用scriplet和((HttpServlet)页).log是一个隐式方法,this.log是userdefine方法。我想知道他们之间的区别 – jayraj 2014-10-08 13:15:41

回答

1

如果你看看从JSP生成的Servlet,那么你会发现thispage都是相同的。这里page是和JSP中的implicit object

生成的Servlet代码从JSP:

public void _jspService(HttpServletRequest request, HttpServletResponse response) 
    throws java.io.IOException, ServletException { 

    PageContext pageContext = null; 
    HttpSession session = null; 
    ServletContext application = null; 
    ServletConfig config = null; 
    JspWriter out = null; 
    Object page = this;   // page and this are same 
    JspWriter _jspx_out = null; 
    PageContext _jspx_page_context = null; 
    ... 

log()方法在GenericServlet定义。这里是tomcat的(Apache)的具体产生的Servlet从JSP实现:

javax.servlet.GenericServlet 
    extended byjavax.servlet.http.HttpServlet 
     extended byorg.apache.jasper.runtime.HttpJspBase 

所以this.log("log message")相当于((HttpServlet)page).log("anothermessage")