JSP Faqs: 1. Best Suitable For Processing Logic
JSP Faqs: 1. Best Suitable For Processing Logic
Servlets JSP
1. Best suitable for processing logic 1. Best suitable for presentation logic
2. we cannot separate business and 2. Separation of presentation and business
presentation logic logic is possible
3. Servlet developer should have strong 3.JSP author is not required to have
knowledge in Java strong knowledge in Java
4. For source code changes ,we have to 4. For source code changes ,it is not
perform explicitly compilation required to perform explicit compilation
5. Relatively development time is more 5. Relatively development time is less
The big difference between both of these technologies lies with the design of the software. JSP
technology is server and platform independent whereas ASP relies primarily on Microsoft technologies.
If the JSP is secured resource then we can place inside WEB-INF folder so that end user is not allowed
to access directly by the name. We can provide the url pattern by configuring in web.xml
<web-app>
<servlet>
<servlet-name>Demo JSP</servlet-name>
<jsp-file>/WEB-INF/test.jsp</jsp-file>
<sevlet>
<servlet-mapping>
<servlet-name>Demo JSP</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
..
</web-app>
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.
The jspInit()- The container calls the jspInit() to initialize te servlet instance.It is called before any
other method, and is called only once for a servlet instance.
The _jspservice()- The container calls the _jspservice() for each request, passing it the request and
the response objects.
The jspDestroy()- The container calls this when it decides take the instance out of service. It is the
last method called n the servlet instance.
JSP contains three life cycle methods namely jspInit( ), _jspService() and jspDestroy(). In these,
jspInit() and jspDestroy() can be overridden and we cannot override _jspService().
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.
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() {
. . .
}
%>
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.
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.
It removes the start-up lag that occurs when a container must translate a JSP page upon receipt of
the first request.
Syntax:
<%
%>
The java code present in the scriptlet will be placed directly inside _jspService() method .
27. What is the difference b/w variable declared inside a declaration and variable declared
in scriplet ?
Variable declared inside declaration part is treated as a instance variable and will be placed directly at
class level in the generated servlet.
Variable declared in a scriptlet will be placed inside _jspService() method of generated servlet.It acts
as local variable.
<%
int k = 10;
%>
What is a Expression?
Syntax:
<%= java expression %>
Eg: <%= new java.util.Date() %>
28.What are the three kinds of comments in JSP and what's the difference between them?
1. JSP Comment:
1. HTML Comment:
This is also known as template text comment or output comment. It is visible in all phases of JSP
including source code of generated response.
1. Java Comments.
<%
// single line java comment
/* this is multiline comment */
%>
This type of comments also known as scripting comments and these are visible in the generated
servlet also.
This is also known as JSP comment and it is visible only in the JSP and in rest of phases of JSP life
cycle it is not visible.
Scripting is disabled by setting the scripting-invalid element of the deployment descriptor to true. It is
a subelement of jsp-property-group. Its valid values are true and false. The syntax for disabling
scripting is as follows:
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
Implicit objects are by default available to the JSP. Being JSP author we can use these and not
required to create it explicitly.
1. request
2. response
3. pageContext
4. session
5. application
6. out
7. config
8. page
9. exception
You can use the errorPage attribute of the page directive to have uncaught run-time
exceptions automatically forwarded to an error processing page.
For example:
<%@ page errorPage="error.jsp" %> redirects the browser to the JSP page error.jsp if an uncaught
exception is encountered during request processing.
Within error.jsp, if you indicate that it is an error-processing page, via the directive:
34. How can I implement a thread-safe JSP page? What are the advantages and
Disadvantages of using it?
You can make your JSPs thread-safe by having them implement the SingleThreadModel
interface. This is done by adding the directive in the JSP.
The generated servlet can handle only one client request at time so that we can make JSP as thread
safe. We can overcome data inconsistency problems by this approach.
ServletContext: Gives the information about the container and it represents an application.
PageContext: Gives the information about the Request and it can provide all other implicit JSP
objects .
Yes, there is. The page implicit object is equivalent to "this", and returns a reference to the generated
servlet.
37 . Can you make use of a ServletOutputStream object from within a JSP page?
Yes . By using getOutputStream() method on response implicit object we can get it.
38 .What is the page directive is used to prevent a JSP page from automatically creating a
session?
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?
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
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.
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
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 >
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.
We can include static files in JSP by using include directive (static include)
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 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:
The response of the jsp will be included in the current page response at request processing time(run
time) hence it is also known as dynamic include.
<%
pageContext.include(“/header.jsp”);
%>
<%
RequestDispatcher rd = request.getRequestDispatcher(“/header.jsp”);
Rd.incliude(request,response);
%>
50.In which situation we can use static include and dynamic include in JSPs ?
If the target resource ( included resource) won’t change frequently, then it is recommended to use
static include.
<%@ include file=”header.jsp” %>
If the target resource(Included page) will change frequently , then it is recommended to use dynamic
include.