jsp生命周期
A JSP page is converted into Servlet in order to service requests. The translation of a JSP page to a Servlet is called Lifecycle of JSP. JSP Lifecycle is exactly same as the Servlet Lifecycle, with one additional first step, which is, translation of JSP code to Servlet code. Following are the JSP Lifecycle steps:
将JSP页面转换为Servlet以便处理请求。 JSP页面到Servlet的转换称为JSP生命周期。 JSP生命周期与Servlet生命周期完全相同,只是第一步是将JSP代码转换为Servlet代码。 以下是JSP生命周期步骤:
Translation of JSP to Servlet code.
将JSP转换为Servlet代码。
Compilation of Servlet to bytecode.
Servlet编译为字节码。
Loading Servlet class.
加载Servlet类。
Creating servlet instance.
创建servlet实例。
Initialization by calling
jspInit()
method通过调用
jspInit()
方法进行初始化Request Processing by calling
_jspService()
method通过调用
_jspService()
方法进行请求处理Destroying by calling
jspDestroy()
method通过调用
jspDestroy()
方法进行销毁
Web Container translates JSP code into a servlet class source(.java) file, then compiles that into a java servlet class. In the third step, the servlet class bytecode is loaded using classloader. The Container then creates an instance of that servlet class.
Web容器将JSP代码转换为servlet类源(.java)文件 ,然后将其编译为Java servlet类。 第三步,使用类加载器加载servlet类字节码。 然后,容器创建该Servlet类的实例。
The initialized servlet can now service request. For each request the Web Container call the _jspService() method. When the Container removes the servlet instance from service, it calls the jspDestroy() method to perform any required clean up.
初始化的servlet现在可以处理请求。 对于每个请求, Web容器都调用_jspService()方法。 当Container从服务中删除servlet实例时,它将调用jspDestroy()方法执行所有必需的清除操作。
将JSP转换为Servlet时会发生什么? (What happens to a JSP when it is translated into Servlet)
Let's see what really happens to JSP code when it is translated into Servlet. The code written inside
is JSP code.
让我们看看将JSP代码转换为Servlet时真正发生了什么。 里面写的代码
是JSP代码。
<html>
<head>
<title>My First JSP Page</title>
</head>
<%
int count = 0;
%>
<body>
Page Count is:
<% out.println(++count); %>
</body>
</html>
The above JSP page(hello.jsp) becomes this Servlet,
上面的JSP页面(hello.jsp)变成了这个Servlet,
public class hello_jsp extends HttpServlet
{
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
PrintWriter out = response.getWriter();
response.setContenType("text/html");
out.write("<html><body>");
int count=0;
out.write("Page count is:");
out.print(++count);
out.write("</body></html>");
}
}
This is just to explain, what happens internally. As a JSP developer, you do not have to worry about how a JSP page is converted to a Servlet, as it is done automatically by the web container.
这只是为了解释内部发生了什么。 作为JSP开发人员,您不必担心JSP页面如何转换为Servlet,因为它是由Web容器自动完成的。
jsp生命周期