Q1) What does JSP stands?
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 use of PrintWriter class. 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.
The JSPs have a translation or a compilation process where the JSP engine translates and compiles a JSP file into a JSP Servlet. The translated and compiled JSP Servlet moves to the execution phase (runtime) where they can handle requests and send responses. Unless explicitly compiled ahead of time, JSP files are compiled the first time they are accessed. On large production sites, or in situations involving complicated JSP files, compilation may cause unacceptable delays to users first accessing the JSP page. The JSPs can be compiled ahead of time (ie precompiled) using application server tools/settings or by writing your own script.
Q2) Explain the life cycle methods of a JSP?
Ans)Declaration Element: is the embedded Java declaration statement, which gets inserted at the Servlet class level.
<%! Calendar c = Calendar.getInstance(); %>
Important: declaring variables via this style is not thread-safe because this variable ends up in the generated Servlet as an instance variable, not within the body of the _jspservice() method. Ensure their access is either read-only or synchronized.
Expression Element: is the embedded Java expression, which gets evaluated by the service method.
<%= new Date()%>
Scriptlet Element: are the embedded Java statements, which get executed as part of the service method.
(Note: Not recommended to use Scriptlet elements because they don't provide reusability and maintainability. Use custom tags (like JSTL, JSF tags, etc) or beans instead).
<%
//Java code
String userName=null;
userName=request.getParameter("userName");
%>
Action Element: A JSP element that provides information for execution phase.
<%@ page import='java.util.Date'%>
<%@ include file=?myJSP %>
<%@ taglib uri=?tagliburi prefix="myTag"%>
Q4) What are the different scope values or what are the different scope values for "jsp:usebean"?
Scope | Object | Comment |
Page | PageContext | Available to the handling JSP page only. |
Request | Request | Available to the handling JSP page or Servlet and forwarded JSP page or Servlet. |
Session | Session | Available to any JSP Page or Servlet within the same session. |
Application | Application | Available to all the JSP pages and Servlets within the same Web Application. |
Q5) What are the differences between static and a dynamic include?
Static include | Dynamic include |
During the translation or compilation phase all the included JSP pages are compiled into a single Servlet. | The dynamically included JSP is compiled into a separate Servlet. It is a separate resource, which gets to process the request, and the content generated by this resource is included in the JSP response. |
No run time performance overhead. | Has run time performance overhead. |
Q6) Is a 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. The following declaration is not thread safe: because these are declarations, and will only be evaluated once when the page is loaded
<%! int a = 5 %>
The following declaration is thread safe: because the variables declared inside the scriplets have the local scope and not shared.<% int a = 5 % >;
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 Create a Custom tag class using only doStartTag()
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 The web.xml deployment descriptor maps the URI to the location of the *.tld (Tag Library Descriptor) file. The code sample web.xml file is shown below:
<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>