1. How do i include static files within a jsp page ?
This
is static include.. you can just include using below code
<%@
include file="header.jsp" %>
or
<%@
include file="footer.html" %>
Dynamic
include you can do like below
<jsp:include
page="header.jsp"/>
or
you can pass parameter also
<jsp:include
page="header.jsp">
<jsp:param
name="title" value="Java Interview Questions"/>
</jsp:include>
2.Difference between static include and dynamic include in JSP ?
The syntax for static include
is <%@ include file=?filename.jsp? %> and the syntax for dynamic
include is <jsp:include page=?filename.jsp? />
Static
include
is an inline inclusion. i.e., the contents of the file will be included at
translation phase. It's something like a copy and paste .
In
case of dynamic include the response of the file will be included to the
original response at runtime. The file will be processed separately and only
the response will become the part of the original files response.
Static
include
cannot accept a parameter (What this parameter will do even if are passing
it?). But dynamic include can accept a parameter. (Here we have some one
to do something on the parameter).
Dynamic
include
<jsp:include
page="masterTemplate.jsp" flush="true">
<jsp:param
name="rColumn" value="rightDisplay.jsp" />
<jsp:param
name="mColumn" value="AlertBody.jsp" />
<jsp:param
name="title" value="Alerts" />
</jsp:include>
we
are passing parameters.
3. which situation you use static include and dynamic include in
jsp?
static
include :
Contents are static like
<%@
include file="header.jsp"%> ..Not changing frequently. Don't
need to pass parameter.
For
example.
a.jsp
contain <%@ include file="header.jsp"%> then all the
codes of header.jsp is included in a.jsp.
dynamic
include :
Need to pass parameter. Need to execute every time. Dynamic content. Excecute
the included jsp and only ouput is pasted to main jsp
For
example..
a.jsp
contain following code
<jsp:include
page="masterTemplate.jsp" flush="true">
<jsp:param
name="rColumn" value="rightDisplay.jsp" />
<jsp:param
name="mColumn" value="AlertBody.jsp" />
<jsp:param
name="title" value="Alerts" />
</jsp:include>
masterTemplate.jsp
execute and output is pasted to a.jsp.
4.Explain the life-cycle mehtods in JSP?
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.
5. How many
JSP scripting elements and what are they?
There
are three scripting language elements:
--declarations
<%!
int
i=8;
%>
--scriptlets
<%
Java Code Logic
i=i+29;
%>
--expressions
<%=
i %> - This will display the value of i.
6. What is a Expression,Declaration,Scriptlet in jsp?
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. Because the value of an expression is converted to a String, you can use
an expression within text in a JSP file. Like
<%=
emp.getName()%>
<%=
(new java.util.Date()).toLocaleString() %>
You
cannot use a semicolon to end an expression.
All
the expression code is converting to servlet. All the expressions go to inside
service() method of the convert servlet.
Declaration
A
declaration declares one or more variables or methods for use later in the JSP
source file.
A
declaration must contain at least one complete declarative statement. You can
declare any number of variables or methods within one declaration tag, as long
as they are separated by semicolons. The declaration must be valid in the
scripting language used in the JSP file.
<%!
int i = 0; %>
<%!
int a, b, c; %
You
can add method to declaration part.
<%!
public
String trimData(String str){
return
str.trim();
}
%>
You
can call the method within the jsp.
All
the declaration code is converting to servlet. If you add method , the method
is in convert servlet. All varibales are instance variable in the convert
servlet.
Scriptlet
Scriptlet
code is like java logic. you can declare varibales in the scriptlet and do the
logic.
All
the Scriptlet go to inside service() method of the convert servlet.
<%
int
n=10;
for(int
i=0;i<n;i++){
}
%>
7.What is a Visible and Hidden Comment in jsp?
On
view source (browser) the Hidden Comment is not visible to end user.
On
view source (browser) the Visible Comment is visible to end user.
Examples
<%--
This comment will not be visible in the page source --%>
<!--
This comment will visible in the page source -->
8.What are implicit objects in JSP?
Certain
objects that are available for the use in jsp documents without being declared
first. These objects are parsed by the JSP engine and inserted into the
generated servlet. The implicit objects re listed below
Ø request
Ø response
Ø pageContext
Ø session
Ø application
Ø out
Ø config
Ø page
Ø exception
9.What are the different scope valiues for the <jsp:useBean>?
Scopes
are
page - with in the same
page
request - after forward or
include also you will get the request scope data.
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.
application - Data will be
available through out the application. One user can store data in application
scope and other can get the data from application scope.
10.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.
11.How does JSP handle run-time exceptions?
You
can use the errorPage attribute of the page directive to have uncaught
runtime exceptions automatically forwarded to an error processing page.
For
example:
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: isErrorPage=true.
the
Throwable object describing the exception may be accessed within the error page
via the exception implicit object.
12. What's the Difference between Forward and Include?
The
<jsp:forward> action enables you to forward the request to
a static HTML file, a servlet, or another JSP.
<jsp:forward
page="url" />
The
JSP that contains the <jsp:forward> action stops processing,
clears its buffer, and forwards the request to the target resource. Note that
the calling JSP should not write anything to the response prior to the <jsp:forward>
action.
You
can also pass additional parameters to the target resource using the <jsp:param>
tag.
<jsp:forward
page="test.htm" >
<jsp:param
name="name1" value="value1" />
<jsp:param
name="name2" value="value2" />
</jsp:forward>
In
this example, test.jsp can access the value of name1 using
request.getParameter("name1").
<jsp:include> execute the code
and force a flush of the buffer in the output stream.
If
a.jsp has the code like
<jsp:include
page="template.jsp" flush="true" >
<jsp:param
name="name1" value="value1" />
</jsp:include>
then
template.jsp execute and the output is placed in a.jsp
13.How you do Session Management in JSP ?
Http
protocol is a stateless protocol, that means that it can't persist the data.
Http treats each request as a new request so every time you will send a request
you will be considered as a new user.
In
session management whenever a request comes for any resource, a unique token is
generated by the server and transmitted to the client by the response object
and stored on the client machine as a cookie. We can also say that the process
of managing the state of a web based client is through the use of session IDs.
Session IDs are used to uniquely identify a client browser, while the server
side processes are used to associate the session ID with a level of access.
Thus, once a client has successfully authenticated to the web applicatiion, the
session ID can be used as a stored authentication voucher so that the client
does not have to retype their login information with each page request. Now
whenever a request goes from this client again the ID or token will also be
passed through the request object so that the server can understand from where
the request is coming.
Session
management can be achieved by :
1.
Cookies: cookies
are small bits of textual information that a web server sends to a browser and
that browsers returns the cookie when it visits the same site again. In cookie
the information is stored in the form of a name, value pair. By default the
cookie is generated. If the user doesn't want to use cookies then it can
disable them browser setting.
2.
URL rewriting: In
URL rewriting we append some extra information on the end of each URL that
identifies the session. This URL rewriting can be used where a cookie is
disabled. It is a good practice to use URL rewriting. In this session ID
information is embedded in the URL, which is recieved by the application
through Http GET requests when the client clicks on the links embedded with a
page.
3.
Hidden form fields:
In hidden form fields the html entry will be like this : <input type
="hidden" name ="name" value="">. This
means that when you submit the form, the specified name and value will be get
included in get or post method. In this session ID information would be
embedded within the form as a hidden field and submitted with the Http POST
method.
4.
HttpSession object :
javax.servlet.http.HttpSession
is an interface that provides a way to identify a user across more than one
page request or visit to a web site. This is the way mainly used in
webapplication. HttpSession object maintain session for you. You don't need to
do any session management.
session.setAttribute("name",name);
String
name = session.getAttribute("name");
you
will get the same value which you have set.
14. How to Disable session in JSP page ?
Disabling
the session in some pages will improve the performance of your JSP container.
Every
time a JSP is requested, JSP creates an HttpSession object to maintain state
for each unique client. The session data is accessible in the JSP as the
implicit session object. In JSPs, sessions are enabled by default.
By
default <%@ page session="true" %>
Session
object uses the server resources. Each session object uses up a small amount of
system resources as it is stored on the server side. This also increases the
traffic as the session ID is sent from server to client. Client also sends the
same session ID along with each request. If some of the JSP pages on your web
site are getting millions of hits from internet browser and there is not need
to identify the user, so its better to disable the session in that JSP page.
You
can tell the container to disable session in the JSP file by setting the
session attribute to false. Set the session attribute of the page directive to
false.
<%@
page session="false" %>
<%@
page language="java" session="false"%>
<html>
<head>
<title>Session
Disabled Example</title>
</head>
<body>
<p>Session
is Disabled in this page </p>
</body>
</html>
15. How do you prevent the Creation of a Session in a JSP Page and
why?
<%@
page session="false">
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.
16. How to add and delete Cookie in jsp ?
Add
Cookie to response object:
Cookie
cookie = new Cookie ("name",value);
cookie.setPath("/");
cookie.setDomain(DOMAIN_NAME);
cookie.setMaxAge(2*
7 * 24 * 60 * 60);// 2 week
response.addCookie(cookie);
Get
cookie from request object :
Cookie
myCookie = null;
Cookie
cookies [] = request.getCookies ();
if
(cookies != null)
for
(int i = 0; i < cookies.length; i++)
{
if
(cookies [i].getName().equals ("name")) // the name of the cookie you
have added
{
myCookie
= cookies[i];
break;
}
}
Delete
Cookie:
You
can't delete the cookie. just add maxage to 0;
cookie.setMaxAge(0);
cookie.setPath("/");
cookie.setDomain(DOMAIN_NAME);
response.addCookie(cookie);
cookie.setMaxAge(-1)
means on browser close cookie will be deleted.
17. How does a servlet communicate with a JSP page?
In
the service method
protected
void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,
java.io.IOException {
User
user = (User)request.getSession().getAttribute("user");
String imgId =
(String)request.getParameter("imgId");
String path =
request.getContextPath()+"/jsp/addnetwork.jsp";
//do some thing
User
user = DAO.getUser(imgId);
request.getSession().setAttribute("user",user);
response.sendRedirect(path);
}
In
the addnetwork.jsp
User
user = getAttribute("user);
No comments:
Post a Comment