Question 1: Explain include Directive and include Action of JSP
Ans: This is a very popular interview question on JSP, which has been asked from long time and still asked in various interview. This question
is good to test some fundamental concept like translation of JSP and
difference between translation time and run time kind of concept.
Syntax for include Directive is <%@ include file="fileName" %>
which means we are including some file to our JSP Page when we use
include directive contents of included file will be added to calling JSP
page at translation time means when the calling JSP is converted to
servlet ,all the contents are added to that page .one important thing is
that any JSP page is complied if we make any changes to that particular
page but if we have changed the included file or JSP page the main
calling JSP page will not execute again so the output will not be
according to our expectation, this one is the main disadvantage of using
the include directive that why it is mostly use to add static
resources, like Header and footer .
Syntax for include action is <jsp:include page=”relativeURL” />
it’s a runtime procedure means the result of the JSP page which is
mentioned in relative URL is appended to calling JSP at runtime on
their response object at the location where we have used this tag
So
any changes made to included page is being effected every time, this is
the main advantage of this action but only relative URL we can use here
,because request and response object is passed between calling JSP and
included JSP.
Question 2: Difference Between include Directive and include Action of JSP
This JSP interview question is a continuation of earlier question I just made it a separate one to write answer in clear tabular format.
Include Directive
|
Include Action
|
include directive is processed at the translation time
|
Include action is processed at the run time.
|
include directive can use relative or absolute path
|
Include action always use relative path
|
Include directive can only include contents of resource it will not process the dynamic resource
|
Include action process the dynamic resource and result will be added to calling JSP
|
We can not pass any other parameter
|
Here we can pass other parameter also using JSP:param
|
We cannot pass any request or response object to calling jsp to included file or JSP or vice versa
|
In this case it’s possible.
|
Question 3: Is it possible for one JSP to extend another java class if yes how?
Ans:
Yes it is possible we can extends another JSP using this <%@ include
page extends="classname" %> it’s a perfectly correct because when
JSP is converted to servlet its implements javax.servlet.jsp.HttpJspPage interface, so for jsp page its possible to extend another java class . This question can be tricky if you don’t know some basic fact J, though its not advisable to write java code in jsp instead its better to use expression language and tag library.
Question 4: What is < jsp:usebean >tag why it is used.
Ans: This was very popular JSP interview question during early 2000, it has lost some of its shine but still asked now and then on interviews.
JSP Syntax
<jsp:useBean
id="beanInstName"
scope="page | request | session | application"
class="package.class" type="package.class"
</jsp:useBean>
id="beanInstName"
scope="page | request | session | application"
class="package.class" type="package.class"
</jsp:useBean>
This
tag is used to create a instance of java bean first of all it tries to
find out the bean if bean instance already exist assign stores a
reference to it in the variable. If we specified type, gives the Bean
that type.otherwise instantiates it from the class we specify, storing a
reference to it in the new variable.so jsp:usebean is simple way to
create a java bean.
Example:
<jsp:useBean id="stock" scope="request" class="market.Stock" />
<jsp:setProperty name="bid" property="price" value="0.0" />
a
<jsp:useBean> element contains a <jsp:setProperty> element
that sets property values in the Bean,we have
<jsp:getProperty>element also to get the value from the bean.
Explanation of Attribute
id="beanInstanceName"
A
variable that identifies the Bean in the scope we specify. If the Bean
has already been created by another <jsp:useBean> element, the
value of id must match the value of id used in the original
<jsp:useBean> element.
The scope in which the Bean exists and the variable named in id is available. The default value is page. The meanings of the different scopes are shown below:
- page – we can use the Bean within the JSP page with the <jsp:useBean> element
- request – we can use the Bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another file.
- session – we can use the Bean from any JSP page in the same session as the JSP page that created the Bean. The Bean exists across the entire session, and any page that participates in the session can use it..
- application – we can use the Bean from any JSP page in the same application as the JSP page that created the Bean. The Bean exists across an entire JSP application, and any page in the application can use the Bean.
Instantiates
a Bean from a class, using the new keyword and the class constructor.
The class must not be abstract and must have a public, no-argument
constructor.
If
the Bean already exists in the scope, gives the Bean a data type other
than the class from which it was instantiated. If you use type without
class or beanName, no Bean is instantiated.
Question 5: How can one Jsp Communicate with Java file.
Ans:we have import tag <%@ page import="market.stock.*” %> like this we can import all the java file
to our jsp and use them as a regular class another way is servlet can
send the instance of the java class to our jsp and we can retrieve
that object from the request obj and use it in our page.
Question 6: what are the implicit Object
Ans: This is a fact based interview question
what it checks is how much coding you do in JSP if you are doing it
frequently you definitely know them. Implicit object are the object that
are created by web container provides to a developer to access them in
their program using JavaBeans and Servlets. These objects are called
implicit objects because they are automatically instantiated.they are
bydefault available in JSP page.
They are: request, response, pageContext, session, and application, out, config, page, and exception.
Question 7: In JSP page how can we handle runtime exception?
Ans: This is another popular JSP interview question
which has asked to check how candidate used to handle Error and
Exception in JSP. We can use the errorPage attribute of the page
directive to have uncaught run-time exceptions automatically forwarded
to an error processing page.
Example: <%@ page errorPage="error.jsp" %>
It will redirect the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, will have to indicate that it is an error-processing page, using the directive: <%@ page isErrorPage="true" %>
Question 8: Why is _jspService() method starting with an '_' while other life cycle methods do not?
Ans:
main JSP life cycle method are jspInit() jspDestroy() and _jspService()
,bydefault whatever content we write in our jsp page will go inside the
_jspService() method by the container if again will try to override
this method JSP compiler will give error but we can override other two
life cycle method as we have implementing this two in jsp so making this
difference container use _ in jspService() method and shows that we
cant override this method.
Question 9: How can you pass information form one jsp to included jsp:
Ans: This JSP interview question is little tricky and fact based. Using < Jsp: param> tag we can pass parameter from main jsp to included jsp page
Example:
<jsp:include page="newbid.jsp" flush="true">
<jsp:param name="price" value="123.7"/>
<jsp:param name="quantity" value="4"/>
<jsp:param name="price" value="123.7"/>
<jsp:param name="quantity" value="4"/>
Question 10: what is the need of tag library?
Ans
tag library is a collection of custom tags. Custom actions helps
recurring tasks will be handled more easily they can be reused across
more than one application and increase productivity. JSP tag libraries
are used by Web application designers who can focus on presentation
issues rather than being concerned with how to access databases and
other enterprise services. Some of the popular tag libraries are Apache
display tag library and String tag library. You can also check my post
on display tag library example on Spring.
| 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.
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 (run time) where they can handle requests and send response. 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) Pre-translated: Before the JSP file has been translated and compiled into the Servlet.
Translated: The JSP file has been translated and compiled as a Servlet. Initialized: Prior to handling the requests in the service method the container calls the jspInit() to initialize the Servlet. Called only once per Servlet instance. Servicing: Services the client requests. Container calls this method for each request. Out of service: The Servlet instance is out of service. The container calls the jspDestroy() method. |
|||||||||||||||
| Q3) What are different type of scripting elements?
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 element 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 Elements: 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 codes String userName=null; userName=request.getParameter("userName"); %> Action Elements: A JSP element that provides information for execution phase. <jsp:useBean id="object_name" class="class_name"/> <jsp:include page="scripts/login.jsp" /> Directive Elements: A JSP element that provides global information for the translation 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"?
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.
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()
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 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> |
No comments:
Post a Comment