Where in a compiled servlet do various parts of a JSP file go?

前端 未结 2 1098
旧巷少年郎
旧巷少年郎 2020-12-21 09:27

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 s

2条回答
  •  猫巷女王i
    2020-12-21 10:12

    Here is an example:

    This JSP code:

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

    Will get translated to:

    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);
        }
    }
    

    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.

提交回复
热议问题