Tuesday 30 October 2012

JSP INTERVIEW QUESTIONS

These are collections of interview questions and answers regarding JSP.


What is  JSP and its used for?
Java Server Pages (JSP) is a platform independent presentation layer technology that comes with SUN s J2EE platform. JSPs are normal HTML pages with Java code pieces embedded in them. JSP pages are saved to *.jsp files. A JSP compiler is used in the background to generate a Servlet from the JSP page.

What are the two kinds of comments in JSP?
<%-- JSP Comment --%>
<!-- HTML Comment -->

What is JSP technology?
Java Server Page is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages. JSPs are secure, platform-independent, and best of all, make use of Java as a server-side scripting language.
What is JSP page?
A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.

How many  implicit objects in JSP?
Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:
1. request
2. response
3. pageContext
4. session
5. application
6. out
7. config
8. page 
9. exception
How many JSP scripting elements and what are they?
There are three scripting language elements:
--declarations
--scriptlets
--expressions

Why are JSP pages preferred API for creating a web-based client program?
Because no plug-ins or security policy files are needed on the client systems(applet does). Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design. This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.

Is  JSP technology extensible?
YES. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.

Can we use the constructor, instead of init(), to initialize servlet?
Yes , of course you can use the constructor instead of init(). There’s nothing to stop you. But you shouldn’t. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.

Can you explain What is JSP page life cycle?
When first time a JSP page is request necessary servlet code is generated and loaded in the servlet container. Now until the JSP page is not changed the compiled servlet code serves any request which comes from the browser. When you again change the JSP page the JSP engine again compiles a servlet code for the same.
JSP page is first initialized by jspInit() method. This initializes the JSP in much the same way as servlets are initialized, when the first request is intercepted and just after translation.

Every time a request comes to the JSP, the container generated _jspService() method is invoked, the request is processed, and response generated.

When the JSP is destroyed by the server, the jspDestroy() method is called and this can be used for clean up purposes.

How do I include static files within a JSP page?
Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Do note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request.
Why does JComponent have add() and remove() methods but Component does not?
because JComponent is a subclass of Container, and can contain other components and jcomponents. How can I implement a thread-safe JSP page? - You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe="false" % > within your JSP page.
How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.
How do you restrict page errors display in the JSP page?
You first set "Errorpage" attribute of PAGE directory to the name of the error page (ie Errorpage="error.jsp")in your jsp page .Then in the error jsp page set "isErrorpage=TRUE". When an error occur in your jsp page it will automatically call the error page.
What do you understand by JSP Actions?
JSP actions are XML tags that direct the server to use existing components or control the behavior of the JSP engine. JSP Actions consist of a typical (XML-based) prefix of "jsp" followed by a colon, followed by the action name followed by one or more attribute parameters.
There are six JSP Actions:
<jsp:include/>
<jsp:forward/>
<jsp:plugin/>
<jsp:usebean/>
<jsp:setProperty/>
<jsp:getProperty/> 

What is the difference between <jsp:include page = ... > and
<%@ include file = ... >?.
Both the tag includes the information from one page in another. The differences are as follows:
<jsp:include page = ... >: This is like a function call from one jsp to another jsp. It is executed ( the included page is executed  and the generated html content is included in the content of calling jsp) each time the client page is accessed by the client. This approach is useful to for modularizing the web application. If the included file changed then the new content will be included in the output.

<%@ include file = ... >: In this case the content of the included file is textually embedded in the page that have <%@ include file=".."> directive. In this case in the included file changes, the changed content will not included in the output. This approach is used when the code from one jsp file required to include in multiple jsp files.
  
What is the difference between <jsp:forward page = ... > and
response.sendRedirect(url),?.

The <jsp:forward> element forwards the request object containing the client request information from one JSP file to another file. The target file can be an HTML file, another JSP file, or a servlet, as long as it is in the same application context as the forwarding JSP file.
sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page. The  response.sendRedirect kills the session variables.
 

implicit Objects
Implicit objects are the objects available to the JSP page. These objects are created by Web container and contain information related to a particular request, page, or application. The JSP implicit objects are:
Variable
Class
Description
application
javax.servlet.ServletContext
The context for the JSP page's servlet and any Web components contained in the same application.
config
javax.servlet.ServletConfig
Initialization information for the JSP page's servlet.
exception
java.lang.Throwable
Accessible only from an error page.
out
javax.servlet.jsp.JspWriter
The output stream.
page
java.lang.Object
The instance of the JSP page's servlet processing the current request. Not typically used by JSP page authors.
pageContext
javax.servlet.jsp.PageContext
The context for the JSP page. Provides a single API to manage the various scoped attributes.
request
Subtype of javax.servlet.ServletRequest
The request triggering the execution of the JSP page.
response
Subtype of javax.servlet.ServletResponse
The response to be returned to the client. Not typically used by JSP page authors.
session
javax.servlet.http.HttpSession
The session object for the client.

What are all the different scope values for the <jsp:useBean> tag?
<jsp:useBean> tag is used to use any java object in the jsp page. Here are the scope values for <jsp:useBean> tag:
a) page
b) request
c) session and
d) application
  
What is JSP Output Comments?
JSP Output Comments are the comments that can be viewed in the HTML source file.
Example:
<!-- This file displays the user login screen -->
and
<!-- This page was loaded on
<%= (new java.util.Date()).toLocaleString() %> -->
 
What is expression in JSP?
Expression tag is used to insert Java values directly into the output. Syntax for the Expression tag is:
<%= expression %>
An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. The following expression tag displays time on the output:
<%=new java.util.Date()%>
  
What types of comments are available in the JSP?
There are two types of comments are allowed in the JSP. These are hidden and outputcomments. A hidden comments does not appear in the generated output in the html, while output comments appear in the generated output.
Example of hidden comment:
<%-- This is hidden comment --%>
Example of output comment:
<!-- This is output comment -->
 
What is JSP declaration?
JSP Decleratives are the JSP tag used to declare variables. Declaratives are enclosed in the <%! %> tag and ends in semi-colon. You declare variables and functions in the declaration tag and can use anywhere in the JSP. Here is the example of declaratives:
<%@page contentType="text/html" %>
<html>
<body>
<%!
int cnt=0;
private int getCount(){
//increment cnt and return the value
cnt++;
return cnt;
}
%>
<p>Values of Cnt are:</p>
<p><%=getCount()%></p>
</body>
</html>
  
What is JSP Scriptlet?
JSP Scriptlet is jsp tag which is used to enclose java code in the JSP pages. Scriptlets begins with <% tag and ends with %> tag. Java code written inside scriptlet executes every time the JSP is invoked.
Example:
  <%
  //java codes
   String userName=null;
   userName=request.getParameter("userName");
   %>
  
What are the life-cycle methods of JSP?
Life-cycle methods of the JSP are:
a) jspInit(): The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance.
b)_jspService(): The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann't be overridden.
c) jspDestroy(): The container calls this when its instance is about to destroyed.
The jspInit() and jspDestroy() methods can be overridden within a JSP page.

No comments:

Post a Comment