Q.1)What is JSP?
Java Server Pages (JSP) is a platform independent presentation layer
technology.JSPs are normal HTML
pages with Java code pieces embedded in them. A JSP compiler is used in the background to generate a
Servlet from the JSP page.
Q.2) Explain the life cycle methods of a JSP?
A)Pre-translated:Before the JSP file has been translated and compiled into the Servlet. B)Translated:The JSP file has been translated and compiled as a Servlet.
C)Initialized:Prior to handling there quests in the service method the container calls the jsp Init() to initialize the Servlet. Called only once per Servlet instance.
D)Servicing: Services the client requests. Container calls the _jspService() method for each request. E)Outofservice:The Servlet instance is out of service.The container calls the jspDestroy() method.
Q.3)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.
Q.4)What are the main elements of JSP?
There are two types of data in a JSP page.
Static part(i.e.HTML,CSSetc),which gets copied directly to the response by the JSP Engine.
Dynamic part,which contains anything that can be translated and compiled by the JSPEngine.
There are three types of dynamic elements.
A)Scripting Elements:
B)Action Elements:
C)Directive Elements:
A)Scripting Elements: A JSP element that provides embedded Java statements. There are three types of scripting elements.
1)Declaration Element: is the embedded Java declaration statement, which gets inserted at the Servlet class level.
<%! int a =10 ;%>
2)Expression Element: is the embedded Java expression, which gets evaluated by the service method.
<%= new date()%>
3)Scriptlet Element: are the embedded Java statements, which get executed as part of the service method.
<%
String username = null;
username = request.getParameter("userName");
%>
B)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" />
C)Directive Elements: A JSP element that provides global information for the translation phase. There are three types of directive elements.
1)page : <%@ page import=”java.util.Date” %>
2)include: <%@ include file=”myJSP” %>
3)taglib: <%@ taglib uri=”tagliburi” prefix=”myTag”%>
Q.5) Can you declare a method within your JSP page?
You can declare methods within your JSP pages as declarations, and your methods can be invoked from within your other methods you declare, expression elements or scriptlets. These declared methods do not have direct access to the JSP implicit objects like session, request,
response etc but you can pass them to your methods you declare as parameters.
<%!
public String myJspMethod(HttpSession session) {
String str = (String)session.getAttribute("anyAttributeName");
return str.substring(0,10);
}
%>
Q.6)How will you perform a browser redirection from a JSP page?
<% response.sendRedirect(“http://www.someAbsoluteAddess.com”); %>
or you can alter the location HTTP header attribute as follows:
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader(“Location”, “/someNewPath/index.html”);
%>
Q.7)How do you prevent the HTML output of your JSP page being cached?
<%
response.setHeader(“Cache-Control”, “no=store”); //HTTP 1.1
response.setHeader("Pragma\","no-cache"); //HTTP 1.0
response.setDateHeader(“Expires”, 0);
%>
Q.8)How do you forward a request to another resource (e.g. another Servlet) from within your JSP?
<jsp:forward page=”/newPage.jsp” />
Q.9)How does JSP handle run-time exceptions?
You can use the attribute “errorPage” of the “page” directive to have your uncaught RuntimeExceptions automatically forwarded to an error processing page.
<%@ page errorPage="error.jsp" %>
The above code redirects the browser client to the error.jsp page. Within your error.jsp page, you need to indicate that it is an error processing page with the “isErrorPage” attribute of the “page” directive as shown below.“exception” is an implicit object accessible only within error pages (i.e. pages with directive <%@ page
isErrorPage=”true” %>
<%@ page isErrorPage=”true” %>
<body>
<%= exception.gerMessage() %>
</body>
Q.10)How will you specify a global error page as opposed to using “errorPage” and “isErrorPage” attributes?
You could specify your error page in the web.xml deployment descriptor as shown below:
By error type:-
<error-page> <exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
By error code:-
<error-page> <error-code>404</error-code>
<location>/error404.html</location>
</error-page>
Q.11)Q. How can you prevent the automatic creation of a session in a JSP page?
Sessions consume resources and if it is not necessary, it should not be created. By default, a JSP page will automatically create a session for the request if one does not exist. You can prevent the creation of useless sessions with the attribute “session” of the page directive.
<%@ page session=”false” %>
Q.12)What are the different scope values ?
1)page
2)request
3)session
4)application
Q.13)What are the differences between static and a dynamic include?
Static include <%@ include %> :-During the translation or compilation phase all the included JSP pages are compiled into a single Servlet.No run time performance overhead.
Dynamic include <jsp:include .....>:-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.Has run time performance overhead.
Q.14)What are implicit objects and list them?
request
response
pageContext
session
application
out
config
page
exception
Q.15)Explain hidden and output comments?
An output comment is a comment that is sent to the client where it is viewable in the browser’s source.
<!-- This is a comment which is sent to the client -->
A hidden comment documents a JSP page but does not get sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags.
<%-- This comment will not be visible to the client --%>
Q.16)Is JSP variable declaration thread safe?
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 declarations end up in the generated servlet as instance variables.
<%! int a = 5 %>
The following declaration is thread safe: because the variables declared inside the scriplets end up in the generated servlet within the body of the _jspService() method as local variables.
<% int a = 5 %>
Q.17)What is the difference between custom JSP tags and JavaBeans?
Custom Tags :-
1 )Can manipulate JSP content
2)Custom tags can simplify the complex operations much better than the bean can. But require a
bit more work to set up.
3)Used only in JSPs in a relatively self-contained manner.
JavaBeans:-
1)Can’t manipulate JSP content.
2)Easier to set up.
3)Can be used in both Servlets and JSPs. You can define a bean in one Servlet and use them in
another Servlet or a JSP page.
Q.18)How will you avoid scriptlet code in JSP?
Use JavaBeans or custom tags instead.
Q:19)Difference between forward and sendRedirect?
When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.
Q.20) Generally you would be invoking a JSP page from a Servlet. Why would you want to invoke a Servlet from a JSP?
JSP technology is intended to simplify the programming of dynamic textual content. If you want to output any binary data (e.g. pdfs, gifs etc) then JSP pages are poor choice for the following reasons and should use Servlets instead:
1)There are no methods for writing raw bytes in the JspWriter object.
2)During execution, the JSP engine preserves whitespace. Whitespace is sometimes unwanted (a .gif file).
Q.21)How can I enable session tracking for JSP pages if the browser has disabled cookies?
using URL rewriting
hidden form field
Q.22) 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.
Q.23) Identify the advantages of JSP over Servlet.
a) Embedding of Java code in HTML pages
b) Platform independence
c) Creation of database-driven Web applications
d) Server-side programming capabilities
Q.24)How can I set a cookie and delete a cookie from within a JSP page?
A cookie, mycookie, can be deleted using the following scriptlet:
<%
//creating a cookie
Cookie mycookie = new Cookie("aName","aValue");
response.addCookie(mycookie);
//delete a cookie
Cookie killMyCookie = new Cookie("mycookie", null);
killMyCookie.setMaxAge(0);
killMyCookie.setPath("/");
response.addCookie(killMyCookie);
%>
Q.25): Can I generate and manipulate JSP pages using XML tools?
The JSP 1.1 specification describes a mapping between JSP pages and XML documents.The mapping enables the creation and manipulation of JSP pages using XML tools.
Q.26)Is it possible to pass in any Object other than a String to a taglib?
Yes, just use an expression. The value of the expression will not be converted to a string in this case.
Take the following example
<example:tag date="<%= new java.util.Date()%>" />
In the above example, the corresponding tag handler will have the setDate method called with a
java.util.Date object passed in as a parameter.
This is perfectly valid in your JSP assuming the tag's value can be a runtime expression. The TLD
for this taglibrary would have to include the following snippet.
<attribute>
<name>date</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
Q.27)Can you have your JSP-generated servlet subclass your own servlet instead of the default HttpServlet?
Your JSP generated servlet can extend your own servlet with the directive:"extend".Your own superclass servlet has to fulfill the contract with the JSP engine by :-
1. Implementing the HttpJspPage interface for HTTP protocol or JspPage interface. If you do not then you will have to make sure that all your super-class servlet methods are declared as final.
2. Implementing your super-class servlet methods as follows:
The service() method has to invoke the _jspService() method. The init() method has to invoke the jspInit() method. The destroy() method has invoke jspDestroy() method.
If the above conditions are not met, then a translation error may be thrown by your JSP engine.
No comments:
Post a Comment