JSP Interview questions |
|||||||||||||||
Q1) What is a JSP? What is it used for? What do you understand by the term JSP translation phase or compilation phase? Ans) JSP (Java ServerPages) is an extension of the Java Servlet technology. JSP is commonly used as the presentation layer for combining HTML and Java code. While Java Servlet technology is capable of generating HTML with out.println(“….. �) statements, where out is a PrintWriter. This process of embedding HTML code with escape characters is cumbersome and hard to maintain. The JSP technology solves this by providing a level of abstraction so that the developer can use custom tags and action elements, which can speed up Web development and are easier to maintain. |
|||||||||||||||
Q2) Explain the life cycle methods of a JSP? Ans) Pre-translated: Before the JSP file has been translated and compiled into the Servlet. |
|||||||||||||||
Q3) What are different type of scripting elements? Ans) |
|||||||||||||||
Q4) What are the different scope values or what are the different scope values for "jsp:usebean"? Ans)
|
|||||||||||||||
Q5) What are the differences between static and a dynamic include? Ans)
|
|||||||||||||||
Q6) Is JSP variable declaration thread safe? Ans) No. The declaration of variables in JSP is not thread-safe, because the declared variables end up in the generated Servlet as an instance variable, not within the body of the _jspservice() method. |
Q7) Explain JSP URL mapping? What is URL hiding or protecting the JSP page? Ans) The JSP resources usually reside directly or under subdirectories (e.g. myPath) of the document root, which are directly accessible to the user through the URL. If you want to protect your Web resources then hiding the JSP files behind the WEB-INF directory can protect the JSP files, css (cascading style sheets) files, Java Script files, pdf files, image files, html files etc from direct access. The request should be made to a servlet who is responsible for authenticating and authorising the user before returning the protected JSP page or its resources. |
Q8) What are custom tags? Explain how to build custom tags? Ans) Custom JSP tag is a tag you define. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. So basically it is a reusable and extensible JSP only solution. The pre-built tags also can speed up Web development.
Step 1
package myTagPkg;
public class MyTag extends TagSupport { int attr = null; public int setAttr(int a ttr){this.attr = a ttr} public int getAttr(){return attr;} public int doStartTag() throws JspException { ....... return 0; } public void release(){.....} } Step 2 The Tag library descriptor file (*.tld) maps the XML element names to the tag implementations. The code sample MyTagDesc.tld is shown below:
<taglib>
<tag> <name>tag1</name> <tagclass>myTagPkg.MyTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>attr</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
Step 3
<web-app>
<taglib> <taglib-uri>/WEB-INF/MyTagURI</taglib-uri> <taglib-location>/WEB-INF/tags/MyTagDesc.tld</taglib-location> </taglib> </web-app> STEP: 4
The JSP file declares and then uses the tag library as shown below:
<%@ taglib uri="/WEB-INF/ MyTagURI" prefix="myTag" %>
< myTag:tag1 attr=�abc� />
<taglib>
<tag> <name>tag1</name> <tagclass>myTagPkg.MyTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>attr</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib> |