2 - Explain The Benefits of JSP?
2 - Explain The Benefits of JSP?
Servlets
JSP
..
</web-app>
10. Explain JSP API ?
The JSP API contains only one package : javax.servlet.jsp
It contains the following 2 interfaces:
1. JspPage:
This interface defines the two life cycle methods jspInit() and jspDestroy().
1. HttpJspPage:
This interface defines only one life cyle method _jspService() method.
Every generated servlet for the jsps should implement either JspPage or HttpJspPage
interface either directly or indirectly.
11. What are the lifecycle phases of a JSP?
Life cycle of JSP contains the following phases:
1.
2.
3.
4.
5.
6.
7.
Webcontainer always generate _jspService() method with JSP content. If we are writing
_jspService() method , then generated servlet contains 2 _jspService() methods which will cause
compile time error.
To show this difference _jspService() method is prefixed with _ by the JSP container and the
other two methods jspInit() and jspDestroy() has no special prefixes.
14 What is the jspInit() method?
The jspInit() method of the javax.servlet.jsp.JspPage interface is similar to the init() method of
servlets. This method is invoked by the container only once when a JSP page is initialized. It can
be overridden by a page author to initialize resources such as database and network connections,
and to allow a JSP page to read persistent configuration data.
15. What is the _jspService() method?
SThe _jspService() method of the javax.servlet.jsp.HttpJspPage interface is invoked every time a
new request comes to a JSP page. This method takes the HttpServletRequest and
HttpServletResponse objects as its arguments. A page author cannot override this method, as its
implementation is provided by the container.
16. What is the jspDestroy() method?
The jspDestroy() method of the javax.servlet.jsp.JspPage interface is invoked by the container
when a JSP page is about to be destroyed. This method is similar to the destroy() method of
servlets. It can be overridden by a page author to perform any cleanup operation such as closing
a database connection.
17. What JSP lifecycle methods can I override?
We can override jspInit() and jspDestroy() methods but we cannot override _jspService()
method.
18. How can I override the jspInit() and jspDestroy() methods within a JSP page?
By using JSP declation tag
<%!
public void jspInit() {
...
}
%>
<%!
public void jspDestroy() {
...
}
%>
19 . Explain about translation and execution of Java Server pages?
A java server page is executed within a Java container. A Java container converts a Java file into
a servlet. Conversion happens only once when the application is deployed onto the web server.
During the process of compilation Java compiler checks for modifications if any modifications
are present it would modify and then execute it.
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. How to pre-compile JSP?
Add jsp_precompile as a request parameter and send a request to the JSP file. This will make
the jsp pre-compile.
http://localhost:8080/jsp1/test.jsp?jsp_precompile=true
It causes excution of JSP life cycle until jspInit() method without executing _jspService()
method.
22. The benefits of pre-compiling a JSP page?
It removes the start-up lag that occurs when a container must translate a JSP page upon receipt of
the first request.
23.How many JSP scripting elements and explain them?
Inside JSP four types of scripting elements are allowed.
1. Scriptlet <% any java code
Can be used to place java code.
%>
<%
Any java code
%>
The java code present in the scriptlet will be placed directly inside _jspService() method .
25. What is a JSP declarative?
JSP declarations are used to declare class variables and methods (both instance and static) in a
JSP page. These declations will be placed directly at class level in the generated servlet and these
are available to the entire JSP.
Syntax:
<%! This is my declarative %>
Eg:
session object is by default available to the JSP. We can make it unavailable by using page
directive as follows.
<%@ page session="false">
39. What's a better approach for enabling thread-safe servlets and JSPs?
SingleThreadModel Interface or Synchronization?
Synchronized keyword is recommended to use to get thread-safety.
40. What are various attributes Of Page Directive ?
Page directive contains the following 13 attributes.
1. language
2. extends
3. import
4. session
5. isThreadSafe
6. info
7. errorPage
8. isError page
9. contentType
10. isELIgnored
11. buffer
12. autoFlush
13. pageEncoding
41 . Explain about autoflush?
This command is used to autoflush the contents. If a value of true is used it indicates to flush the
buffer whenever it is full. In case of false it indicates that an exception should be thrown
whenever the buffer is full. If you are trying to access the page at the time of conversion of a JSP
into servlet will result in error.
42. How do you restrict page errors display in the JSP page?
You first set "errorPage" attribute of PAGE directive 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, then the control will be automatically forward to error
page.
43. What are the different scopes available fos JSPs ?
There are four types of scopes are allowed in the JSP.
1. page - with in the same page
2. request - after forward or include also you will get the request scope data.
3. session - after senRedirect also you will get the session scope data. All data stored in
session is available to end user till session closed or browser closed.
4. application - Data will be available throughout the application. One user can store data
in application scope and other can get the data from application scope.
44. when do use application scope?
If we want to make our data available to the entire application then we have to use application
scope.
45. What are the different scope valiues for the <jsp:useBean>?
The different scope values for <jsp:useBean> are
1. page
2. request
3.session
4.application
46. How do I use a scriptlet to initialize a newly instantiated bean?
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.
<jsp:useBean id="foo" class="com.Bar.Foo" >
<jsp:setProperty name="foo" property="x"
value="<%=java.text.DateFormat.getDateInstance().format(new java.util.Date()) %>" / >
<%-- scriptlets calling bean setter methods go here --%>
</jsp:useBean >
47 . Can a JSP page instantiate a serialized bean?
No problem! The use Bean 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-INF/jspbeans directory for it to be located
by the JSP engine.
48.How do we include static files within a jsp page ?
We can include static files in JSP by using include directive (static include)
<%@ include file=header.jsp %>
The content of the header.jsp will be included in the current jsp at translation time. Hence
this inclusion is also known as static include.
49.In JSPs how many ways are possible to perform inclusion?
In JSP, we can perform inclusion in the following ways.
1. By include directive:
<%@ include file=header.jsp %>
The content of the header.jsp will be included in the current jsp at translation time. Hence
this inclusion is also known as static include.
1. By include action: