jsp生命周期_JSP的生命周期

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生命周期步骤:

  1. Translation of JSP to Servlet code.

    将JSP转换为Servlet代码。

  2. Compilation of Servlet to bytecode.

    Servlet编译为字节码。

  3. Loading Servlet class.

    加载Servlet类。

  4. Creating servlet instance.

    创建servlet实例。

  5. Initialization by calling jspInit() method

    通过调用jspInit()方法进行初始化

  6. Request Processing by calling _jspService() method

    通过调用_jspService()方法进行请求处理

  7. Destroying by calling jspDestroy() method

    通过调用jspDestroy()方法进行销毁

Life Cycle of JSp

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容器自动完成的。

翻译自: https://www.studytonight.com/jsp/lifecycle-of-jsp.php

jsp生命周期

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值