JSP Interview Questions with Answers Page III
From freshersonline.com
1. Can I stop JSP execution while in the midst of processing a request?
Yes. Preemptive termination of request processing on an error condition is a good way to maximize the throughput of
a high-volume JSP engine. The trick (asuming Java is your scripting language) is to use the return statement when you
want to terminate further processing.
2. Can a JSP page process HTML FORM data?
Yes. However, unlike servlets, you are not required to implement HTTP-protocol specific methods like doGet() or doPost()
within your JSP page. You can obtain the data for the FORM input elements via the request implicit object within a
scriptlet or expression as.
3. How do you pass control from one JSP page to another?
Use the following ways to pass control of a request from one servlet to another or one jsp to another.
The RequestDispatcher object ‘s forward method to pass the control.
The response.sendRedirect method
4. Is there a way I can set the inactivity lease period on a per-session basis?
Typically, a default inactivity lease period for all sessions is set within your JSPengine admin screen or associated properties
file. However, if your JSP engine supports the Servlet 2.1 API, you can manage the inactivity lease period on a per-session basis.
This is done by invoking the HttpSession.setMaxInactiveInterval() method, right after the session has been created.
5. 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.
The following example shows the syntax:
< % @ include file="copyright.html" % >
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.
How do I have the JSP-generated servlet subclass my own custom servlet class, instead of the default? One should be very careful
when having JSP pages extend custom servlet classes as opposed to the default one generated by the JSP engine. In doing so, you may
lose out on any advanced optimization that may be provided by the JSPengine.
In any case, your new superclass has to fulfill the contract with the JSP engine by: Implementing the HttpJspPage interface, if the
protocol used is HTTP, or implementing JspPage otherwise Ensuring that all the methods in the Servlet interface are declared final.
Additionally, your servlet superclass also needs to do the following:
The service() method has to invoke the _jspService() method
The init() method has to invoke the jspInit() method
The destroy() method has to invoke jspDestroy()
If any of the above conditions are not satisfied, the JSP engine may throw a translation error.
6. Can you make use of a ServletOutputStream object from within a JSP page?
No. You are supposed to make use of only a JSPWriter object (given to you in the form of the implicit object out) for replying to clients.
A JSPWriter can be viewed as a buffered version of the stream object returned by response.getWriter(), although from an implementational
perspective, it is not.
A page author can always disable the default buffering for any page using a page directive
7. Can a JSP page instantiate a serialized bean?
No problem! The useBean action specifies the beanName attribute, which can be used for indicating a serialized bean.
For example:
A couple of important points to note. Although you would have to name your serialized file "filename.ser", you only indicate
"filename" as the value for the beanName attribute. Also, you will have to place your serialized file within the WEB-INFjspbeans
directory for it to be located by the JSP engine.
8. How do I mix JSP and SSI #include?
Answer 1
If you're just including raw HTML, use the #include directive as usual inside your .jsp file.
But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. If your data.inc
file contains jsp code you will have to use
The is used for including non-JSP files.
Answer 2
If you're just including raw HTML, use the #include directive as usual inside your .jsp file.
But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. Ronel Sumibcay
(ronel@LIVESOFTWARE.COM) says: If your data.inc file contains jsp code you will have to use
<%@ vinclude="data.inc" %>
The is used for including non-JSP files.
9. How do I mix JSP and SSI #include? What is the difference between include directive & jsp:include action?
Difference between include directive and jsp:include action
1. provides the benifits of automatic recompliation,smaller class size ,since the code corresponding to the included page is
not present in the servlet for every included jsp page and option of specifying the additional request parameter.
2.The also supports the use of request time attributes valus for dynamically specifying included page which directive does not.
3.the include directive can only incorporate contents from a static document.
4. can be used to include dynamically generated output eg. from servlets.
5.include directive offers the option of sharing local variables,better run time efficiency.
6.Because the include directive is processed during translation and compilation,it does not impose any restrictions on output buffering.
10. How do you prevent the Creation of a Session in a JSP Page and why? What is the difference between include
directive & jsp:include action?
By default, a JSP page will automatically create a session for the request if one does not exist.
However, sessions consume resources and if it is not necessary to maintain a session, one should not be created. For example,
a marketing campaign may suggest the reader visit a web page for more information. If it is anticipated that a lot of traffic
will hit that page, you may want to optimize the load on the machine by not creating useless sessions.
11. How do I use a scriptlet to initialize a newly instantiated bean?
A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when
the specified bean is instantiated. Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly
instantiated bean, although you are not restricted to using those alone.
The following example shows the "today" property of the Foo bean initialized to the current date when it is instantiated. Note
that here, we make use of a JSP expression within the jsp:setProperty action.
value=""/ >
12. How do you connect to the database from JSP?
A Connection to a database can be established from a jsp page by writing the code to establish a connection using a jsp scriptlets.
Further then you can use the resultset object "res" to read data in the following way.
13. How do you delete a Cookie within a JSP?
Cookie mycook = new Cookie("name","value");
response.addCookie(mycook);
Cookie killmycook = new Cookie("mycook","value");
killmycook.setMaxAge(0);
killmycook.setPath("/");
killmycook.addCookie(killmycook);
14. Can we implement an interface in a JSP?
No
15. What is the difference between ServletContext and PageContext?
ServletContext: Gives the information about the container
PageContext: Gives the information about the Request
16. What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?
request.getRequestDispatcher(path): In order to create it we need to give the relative path of the resource
context.getRequestDispatcher(path): In order to create it we need to give the absolute path of the resource.
17. How to pass information from JSP to included JSP?
Using <%jsp:param> tag.
18. How is JSP include directive different from JSP include action.
When a JSP include directive is used, the included file's code is added into the added JSP page at page translation time,
this happens before the JSP page is translated into a servlet. While if any page is included using action tag, the page's output is
returned back to the added page. This happens at runtime.
19. Can we override the jspInit(), _jspService() and jspDestroy() methods?
We can override jspinit() and jspDestroy() methods but not _jspService().
20. Why is _jspService() method starting with an '_' while other life cycle methods do not?
_jspService() method will be written by the container hence any methods which are not to be overridden by the end user are typically
written starting with an '_'. This is the reason why we don't override _jspService() method in any JSP page.
21. Explain the life cycle of JSP?
Any JSP page goes through 7 phases in its entire lifecycle
Phase Name Description
Page translation The page is parsed and a Java file containing
the corresponding servlet is created.
Page compilation The Java file is compiled.
Load class The compiled class is loaded.
Create instance An instance of the servlet is created.
Call jspInit() This method is called before any other
method to allow initialization.
Call _jspService() This method is called for each request.
Call jspDestroy() This method is called when the servlet container
decides to take the servlet out of service.
22. What happens when a page is statically included in another JSP page?
An include directive tells the JSP engine to include the contents of another file (HTML, JSP, etc.) in the current page.
This process of including a file is also called as static include.
23. Can you override jspInit() method? If yes, In which cases?
ye, we can. We do it usually when we need to intialize any members which are to be available for a servlet/JSP throughout its lifetime.
24. What is the difference between directive include and jsp include?
<%@ include> : Used to include static resources during translation time. : Used to include dynamic content or static content during runtime.
25. What is the difference between RequestDispatcher and sendRedirect?
RequestDispatcher: server-side redirect with request and response objects. sendRedirect : Client-side redirect with new request and
response objects.
26. How does JSP handle runtime exceptions?
Using errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting
of a JSP.
27. How can my application get to know when a HttpSession is removed?
Define a Class HttpSessionNotifier which implements HttpSessionBindingListener and implement the functionality what you need in valueUnbound()
method.
Create an instance of that class and put that instance in HttpSession.
