JSP FAQ

JSP Lifecycle ?

 
  • The jsp file converted in to java file
  • The java file will compiled and generate the class file
  • Thrid load the class file
  • Create the instance of the loaded class using Class.forname('exmpleclass').getInstance()
  • 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. 
  • jspservice() - The container calls the _jspservice() for each request, passing it the request and the response objects. 
  • jspDestroy()- The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance. 

—————

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>

—————

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); DOMAIN_NAME may be .techfaq360.com 
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.

—————

.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.

—————

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

—————

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. 

—————

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. 

—————

What are the implicit objects?

Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are: 

  • request 
  • response 
  • pageContext 
  • session 
  • application 
  • out 
  • config 
  • page 
  • exception

—————

How do I prevent the browser from caching my dynamic content?

add the code to your jsp. 
<% 
response.setHeader( "Cache-Control", "no-cache" ); 
response.setHeader( "Pragma", "no-cache" ); 
response.setIntHeader( "Expires", 0 ); 
%>

—————

JSP scripting elements and what are they?

Directive tag:

 

The directive tag gives special information about the page to JSP Engine. This changes the way JSP Engine processes the page. Using directive tag, user can import packages, define error handling pages or session information of JSP page.


General notation of directive tag is as follows:


 

There are three types of directive tag.


  • page
  • Include
  • Tag Lib

Syntax and usage of directive tag

 

 

 
General syntax for the page directive is
 
<%@ page optional attribute ... %>
 
There are many optional attributes available for page directive. Each of these attributes are used to give special processing information to the JSP Engine changing the way the JSP Engine processes the page. Some of the optional attributes available for page directive are:
 
language
extends
import
session
buffer
autoFlush
isThreadSafe
info
errorPage
IsErrorPage
contentType
Syntax and usage of some of the optional attributes available for page directive discussed below.

 

Declarations  tag

<%! 
int i=8; 
%> 
Scriptlets  tag
<% Java Code Logic 
i=i+29; 
%> 

Expressions tag
<%= i %> - This will display the value of i.

—————


Topic: JSP FAQ

No comments found.