JSP和Servlet生命周期方法 [英] JSP and Servlet life cycle method

查看:155
本文介绍了JSP和Servlet生命周期方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果JSP变成Servlet,为什么有不同的生命周期方法,例如 jspInit() init()

If JSP turns into a Servlet why there are different life cycle methods for example jspInit() and init() ?

推荐答案

我将尝试以最简单的方式正确解释它(作为正确的@Nathan Hughes答案的补充) ):

I'll try to explain it correctly in the most simple way (as addition to the correct @Nathan Hughes answer) :

从HTML与JAVA代码的角度来看,servlet更像是用JAVA包装的HTML。它为处理多层体系结构中的应用层提供了强有力的支持。另一方面,创建JSP以支持表示层的创建。在初始化servlet 期间,只调用一次servlet的 init()方法。

From the perspective of HTML vs JAVA code, servlet is more like HTML wrapped with JAVA. It gives a strong support for handling application layer in the multilayered architecture. On the other hand JSPs were created to support the creating of presentation layer. The init() method of a servlet is called only once during initialization of the servlet.

所以第一点:Servlets在JSP之前就已经存在了。

So the first point : Servlets were here before JSPs.

现在是JSP。再次从HTML和JAVA的角度来看,JSP更像是用HTML包装的JAVA。

Now to JSPs. Again from the perspective of HTML and JAVA, JSP is more like JAVA wrapped with HTML.

WEB CONTAINER 执行JSP源代码到等效Servlet java代码的转换。然后编译这个翻译的java Servlet源代码, WEB CONTAINER 处理Servlet的实现。简单地说:

WEB CONTAINER performs a translation of the JSP "source code" to the equivalent Servlet java code. This translated java Servlet source code is then compiled and the WEB CONTAINER handles the realisation of the Servlet. Simply:

MyPage.jsp - >(翻译) - > MyPage_jsp.java - >(编译) - > MyPage_jsp.class - > (加载) - > Java Servlet

WEB容器调用 jspInit()方法作为JSP生命周期的初始化阶段的一部分。

The jspInit() method is called by WEB CONTAINER as a part of the initialization phase of the JSP lifecycle.

所以问题: jspInit() 不等于 init()

这篇关于JSP和Servlet生命周期方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆