JSP(Java Server Pages) -------------------------- Q)What is JSP? =>JSP is a Java based web technology from Sun Microsystems. Q)What is the purpose of JSP technology? =>JSP technology is used to develop Java based dynamic websites. OR =>In Java based web application development JSP technology is used. Q)What is a jsp? =>A jsp is a dynamic web resource that enahances the functionality of the web server. =>A jsp is a JSP Container managed web server side piece of code(server side program). =>A jsp is a web component. =>A jsp is a a text file with any name but with ".jsp extension. Q)What can a jsp do in a Java web application? =>All that a servlet can do. Q)Can a jsp replace a servlet? =>Yes. But it will not do. Note:- In fact, each use-case is implemented using a servlet and a jsp together. Q)When there is already Servlet technology, what was the need of JSP technology? =>servlets are weak in presentation. JSP is excellent in that. Q)What is the life cycle of a jsp? A JSP page(a jsp) life cycle is described with 3 life cycle methods and 6 life cycle phases. =>Life cycle methods are 1.)jspInit() 2.)_jspService() 3.)jspDestory() =>Life cycle phases are 1.)translation phase 2.)compilation phase 3.)initialization phase 4.)instantiation phase 5.)servicing phase 6.)destruction phase Translation phase -------------------- =>JSP Container converting a jsp into a servlet is nothing but translation phase of a jsp. =>When client request comes for a jsp for the first time after its deployment, JSP engine makes a full read of the source code of the jsp and verifies the syntactical correctness of the JSP elements(if any) used in the jsp. If JSP elements are syntactically correct, it converts ".jsp" file into ".java" file. =>".java" file created during translation phase is nothing but the source code of a servlet. I.e. during translation phase of a jsp, JSP container generates a servlet. This servlet is known as container generated servlet OR page implementation class. Compilation phase --------------------- =>JSP Engine compiling the page implementation class source code to ".class" file is nothing but compilation phase of a jsp. Note:- translation and compilation happens only once in the life time of a jsp unless its source code is modified. Q)Explain about instantiation,initialization,servicing and destruction phase of a jsp. =>Container instantiating the page implementation class is nothing but instantiation phase of the jsp. =>Container calling jspInit() method of the page implementation class is nothing but initialization phase of the jsp. =>Instantiation and initialization happens only once in the life time of a jsp. =>jsp developer(page author) provides any resources required for the jsp in jspInit() method. For eg. database connection creation & PreparedStatement creation. =>Container calling _jspService() method is nothing but servicing phase of the jsp. for each client request Container calls _jspService() method. =>JSP container is responsible to generate _jspService() method. =>In a jsp, jsp developer should not implement _jspService() method. If required, jspInit() & jspDestroy() can be implemented. =>When application is unloaded or Container is shutdown, container calls jspDestroy() and marks the page implementation class instance for garbage collection. =>Container calling jspDestroy() method is nothing but destruction phase of the jsp. =>destruction happens only once in the life time of a jsp. =>resources if any allocated during initialization phase are deallocated in jspDestroy() method. Q)What are the constituents (contents) of a jsp? A jsp= HTML code+Java code+JSP elements Note:- "writing HTML code inside Java code is servlet development and vice-versa is jsp development" Q)Explain about Scripting elements. =>JSP Scripting elements are those JSP elements using which, Java code is directly embeded into a jsp. =>Scripting elements are of 3 types. 1.)declaration 2.)expression 3.)scriptlet declaration ------------- =>JSP declaration is one of the three JSP scripting elements. =>It is used to place Java code directly within the source code of a jsp. =>A JSP declaration starts with <%! and ends with %> =>With in a JSP declaration we can place Java variables and Java methods. For eg. <%! int count; Connection con; PreparedStatement ps; public void jspInit() { ..... } public void jspDestroy() { } public void process() { } %> =>During translation phase, Java variables and Java methods of the JSP declaration become the members of the page implementation class. i.e. container generated servlet. =>We can have any number JSP declarations in a jsp. expression ------------- =>JSP expression is one of the three JSP scripting elements. =>It is used to place Java code directly within the source code of a jsp. =>A JSP expression starts with <%= and ends with %> =>Within a JSP expression we can write only one Java expression(that too without semi colon). For eg. a.) <%= a+b %> b.) <%= request.getParameter("username") %> c.) <%= sum %> =>During translation phase, Java expression of a JSP expression is placed in _jspService() method of page implementation class. =>We can have any number of JSP expressions in a jsp. In the same order their Java code will be placed in _jspService method. =>When a JSP expression is executed, 2 things happen in the background. 1.)Java expression is evaluated. 2.)evaluated value is written to the browser stream. scriptlet ----------- =>JSP scriptlet is one of the three JSP scripting elements. =>It is used to place Java code directly within the source code of a jsp. =>A JSP scriptlet starts with <% and ends with %> =>We can place free form of Java code in a JSP scriptlet. i.e. during servlet programming whatever Java code we write in servicing method to implement the use-case, all that Java code we can write in a JSP scriptlet. For eg. <% String a=request.getParameter("t1"); String b=request.getParameter("t2"); int n1=Integer.parseInt(a); int n2=Integer.parseInt(b); ... %> =>During translation phase, Java code of a JSP Scriptlet is placed in _jspService() method of page implementation class. =>We can have any number of JSP scriptlets in a jsp. In the same order their Java code will be placed in _jspService method.