1. What is JSP and why do we need it?
Java Server Pages technology (JSP) is used to create dynamic web page. It is an extension to the servlet technology. A JSP page is internally converted into servlet.
JSP is actually developed to
1. lower the burden on the developer as the Servlet coding developement will be taken care by jsp compiler and can make use of some implicit variables.
2. allow a non-java programmer can make use of JSP to develop a web application by knowing only a few tags and their usage.
3. to resemble the MVC arch.
2. Explain JSP Compilation Model?
When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page.
The compilation process involves three steps:
Parsing the JSP.
Turning the JSP into a servlet.
Compiling the servlet.
3. Difference between JSP and Servlet?
JSP
JSP is a webpage scripting language that can generate dynamic content.JSP run slower compared to Servlet as it takes compilation time to convert into Java Servlets..It?s easier to code in JSP than in Java Servlets.In MVC, jsp act as a view..JSP are generally preferred when there is not much processing of data required.The advantage of JSP programming over servlets is that we can build custom tags which can directly call Java beans..We can achieve functionality of JSP at client side by running JavaScript at client side.
Servlets
Servlets are Java programs that are already compiled which also creates dynamic web content..Servlets run faster compared to JSP..Its little much code to write here..In MVC, servlet act as a controller.servlets are best for use when there is more processing and manipulation involved.There is no such custom tag facility in servlets.There are no such methods for servlets.
4. Explain JSP Life cycle?
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 consists of following steps.
Translation of JSP to Servlet code.
Compilation of Servlet to bytecode.
Loading Servlet class.
Creating servlet instance.
Initialization by calling jspInit() method
Request Processing by calling _jspService() method
Destroying by calling jspDestroy() method
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.
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.
5. Explain the types of directives in JSP
-The page directive
-The include directive
-The taglib directive
6. How can we avoid direct access of JSP pages from client browser?
7. Difference between HTML and JSP Comment?
JSP Comments are removed by the JSP Engine during the translation phase (JSP Comments are not even part of the compilation unit) whereas the HTML Comments are treated like any other HTML tage and hence they are maintained throughout. This is reason why we can easily see the HTML comments in the browse by viewing the source of the page.
8. Is it possible to use java comment in JSP?
yes
9. What is Scriptlet, Expression and Declaration in JSP?
10. What are JSP implicit objects?
Implicit Objects and their corresponding classes:
out javax.servlet.jsp.JspWriter
request javax.servlet.http.HttpServletRequest
response javax.servlet.http.HttpServletResponse
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
exception javax.servlet.jsp.JspException
page java.lang.Object pageContext javax.servlet.jsp.
PageContext config javax.servlet.ServletConfig
Object page = this;Since page is a variable of type Object, it cannot be used to directly call the servlet methods. To access any of the methods of the servlet through page it must be first cast to type Servlet.
<%= this.getServletInfo(); %>But the following code will generate error, because can not use page directly without casting:
<%= ((Servlet)page).getServletInfo(); %>
<%= page.getServletInfo(); %>Note that page is not the same as the pageContext object
<c:out value="<br> creates a new line in HTML" escapeXml="true"></c:out>
<%@ page isELIgnored="true" %>