JSP文件的各个部分在编译后的servlet中位于何处? [英] Where in a compiled servlet do various parts of a JSP file go?

查看:60
本文介绍了JSP文件的各个部分在编译后的servlet中位于何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在翻译阶段之前,<%! ....%>代码行在我的JSP页面中.我知道在转换后的servlet中只能运行一次.这是否意味着servlet引擎将代码放入servlet的init()方法中?

Before the translation phase, there was a <%! .... %> code line in my JSP page. I know that this would be run only once in the translated servlet. Does it mean that the servlet engine puts the code in the init() method in the servlet?

我想学习的是:翻译后,哪些类型的代码可以使用哪些方法?

What I want to learn is: which kinds of code go to which methods after translation?

谢谢.

推荐答案

以下是示例:

此JSP代码:

<%@ page import="java.util.*" %> <!-- 1 -->
<%! private Date date; %>        <!-- 2 -->
<% date = new Date(); %>         <!-- 3 -->
Current date: <%= date %>        <!-- 4 -->

将被翻译为:

import java.util.*; // 1

public class ServletAbc extends GenericServlet {

    private Date date; // 2

    public void service(ServletRequest request,ServletResponse response)
                throws IOException,ServletException{

        PrintWriter out=response.getWriter();

        date = new Date(); // 3

        out.println("Current date: "); // 4
        out.println(date);
    }
}

请注意,翻译的一小部分取决于容器.例如. out.println()语句也可能会转换为out.println("Current date: " + date);.

Note that minor parts of the translation are container-depended. E.g. the out.println() statements might be translated to out.println("Current date: " + date); as well.

这篇关于JSP文件的各个部分在编译后的servlet中位于何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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