Quick Refresh : JSP


Quick Refresh : JSP

Question: What are implicit Objects available to the JSP Page?
Answer: Implicit objects are the objects available to the JSP page. These objects are created by Web container and contain information related to a particular request, page, or application. The JSP implicit objects are:
Variable
Class
Description
application
javax.servlet.ServletContext
The context for the JSP page's servlet and any Web components contained in the same application.
config
javax.servlet.ServletConfig
Initialization information for the JSP page's servlet.
exception
java.lang.Throwable
Accessible only from an error page.
out
javax.servlet.jsp.JspWriter
The output stream.
page
java.lang.Object
The instance of the JSP page's servlet processing the     current request. Not typically used by JSP page authors.
pageContext
javax.servlet.jsp.PageContext
The context for the JSP page. Provides a single API to manage the various scoped attributes. In JSP, page scope is the default scope.
request
Subtype of javax.servlet.ServletRequest
The request triggering the execution of the JSP page.
response
Subtype of javax.servlet.ServletResponse
The response to be returned to the client. Not typically used by JSP page authors.
session
javax.servlet.http.HttpSession
The session object for the client.

JSP actions : JSP actions are XML tags that direct the server to use existing components or control the behavior of the JSP engine. The action tags basically are used to control the flow between pages and to use Java Bean. JSP Actions consist of a typical (XML-based) prefix of "jsp" followed by a colon, followed by the action name followed by one or more attribute parameters.
There are six JSP Actions:
  • <jsp:include/> - The jsp:include action tag is used to include the content of another resource it may be jsp, html or servlet. The jsp include action tag includes the resource at request time so it is better for dynamic pages because there might be changes in future.
  • <jsp:forward/>  - The jsp:forward action tag is used to forward the request to another resource it may be jsp, html or another resource.<jsp:plugin/> - The jsp:plugin action tag is used to embed applet in the jsp file. The jsp:plugin action tag downloads plugin at client side to execute an applet or bean.
  • <jsp:usebean/> - The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn't create the bean depending on the scope. But if object of bean is not created, it instantiates the bean.
    • id:  is used to identify the bean in the specified scope.
    • scope:  represents the scope of the bean. It may be page, request, session or application. The default scope is page.
      • page:  specifies that you can use this bean within the JSP page. The default scope is page.
      • request:   specifies that you can use this bean from any JSP page that processes the same request. It has wider scope than page.
      • session:   specifies that you can use this bean from any JSP page in the same session whether processes the same request or not. It has wider scope than request.
      • application:   specifies that you can use this bean from any JSP page in the same application. It has wider scope than session.
    • class: instantiates the specified bean class (i.e. creates an object of the bean class) but it must have no-arg or no constructor and must not be abstract.
    • type:  provides the bean a data type if the bean already exists in the scope. It is mainly used with class or beanName attribute. If you use it without class or beanName, no bean is instantiated.
    • beanName:  instantiates the bean using the java.beans.Beans.instantiate() method.
  • <jsp:setProperty/> - The jsp:setProperty action tag sets a property value or values in a bean using the setter method.
  • <jsp:getProperty/>  - The jsp:getProperty action tag returns the value of the property.
JSP Directives: The directives are messages that tells the web container how to translate a JSP page into corresponding servlet.There are three types of directives:
  •  page directive - The page directive defines attributes that apply to an entire JSP page.
<%@ page attribute="value" %>
Attributes of JSP page directive:
    • import - The import attribute is used to import class,interface or all the members of a package.It is similar to import keyword in java class or interface.
    • contentType - The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of the HTTP response.The default value is "text/html;charset=ISO-8859-1".
    • extends - The extends attribute defines the parent class that will be inherited by the generated servlet.It is rarely used.
    • info - This attribute simply sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet interface.
    • buffer - The buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP page.The default size of the buffer is 8Kb.
    • language - The language attribute specifies the scripting language used in the JSP page. The default value is "java".
    • isELIgnored - We can ignore the Expression Language (EL) in jsp by the isELIgnored attribute. By default its value is false i.e. Expression Language is enabled by default. We see Expression Language later.
    • isThreadSafe - Servlet and JSP both are multithreaded.If you want to control this behaviour of JSP page, you can use isThreadSafe attribute of page directive.The value of isThreadSafe value is true.If you make it false, the web container will serialize the multiple requests, i.e. it will wait until the JSP finishes responding to a request before passing another request to it.
    • autoFlush -
    • session
    • pageEncoding
    • errorPage - The errorPage attribute is used to define the error page, if exception occurs in the current page, it will be redirected to the error page.
    • isErrorPage - The isErrorPage attribute is used to declare that the current page is the error page.
  • include directive - The include directive is used to include the contents of any resource it may be jsp file, html file or text file. The include directive includes the original content of the included resource at page translation time (the jsp page is translated only once so it will be better to include static resource).
<%@ include file="resourceName" %>
  • taglib directive - The JSP taglib directive is used to define a tag library that defines many tags. We use the TLD (Tag Library Descriptor) file to define the tags. In the custom tag section we will use this tag so it will be better to learn it in custom tag.
<%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>
Question: What is expression in JSP?
Answer: Expression tag is used to insert Java values directly into the output. Syntax for the Expression tag is: 
<%= 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. The following expression tag displays time on the output:
<%=new java.util.Date()%>  
Question: What types of comments are available in the JSP?
Answer: There are two types of comments are allowed in the JSP. These are 
hidden and output comments. A hidden comments does not appear in the generated output in the html, while output comments appear in the generated output.
Example of hidden comment:
<%-- This is hidden comment --%>
Example of output comment:
<!-- This is output comment -->
Question: What is JSP declaration?
Answer: JSP Decleratives are the JSP tag used to declare variables. Declaratives are enclosed in the <%! %> tag and ends in semi-colon. You declare variables and methods in the declaration tag and can use anywhere in the JSP. 
The code written inside the jsp declaration tag is placed outside the _jspService() method of auto generated servlet. So it doesn't get memory at each request.

Question: What is JSP Scriptlet?
Answer: JSP Scriptlet is jsp tag which is used to enclose java code in the JSP pages. Scriptlets begins with <% tag and ends with %> tag. Java code written inside scriptlet executes every time the JSP is invoked because 
code in scriptlet tag is placed inside the _jspService() method.  The jsp scriptlet tag can only declare variables not methods.
Example:  <%
  //java codes
   String userName=null;
   userName=request.getParameter("userName");
   %>
 
Question: What are the life-cycle methods of JSP?
Answer: Life-cycle methods of the JSP are:
a) jspInit(): The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance.
b)_jspService(): The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann't be overridden.
c) jspDestroy(): The container calls this when its instance is about to destroyed.
The jspInit() and jspDestroy() methods can be overridden within a JSP page.
The JSP pages follows these phases:
  • Translation of JSP Page
  • Compilation of JSP Page
  • Classloading (class file is loaded by the classloader)
  • Instantiation (Object of the Generated Servlet is created).
  • Initialization ( jspInit() method is invoked by the container).
  • Reqeust processing ( _jspService() method is invoked by the container).
  • Destroy ( jspDestroy()  method is invoked by the container).
 
Question: What is the difference between <jsp:include page = ... > and
<%@ include file = ... >?.
Answer: Both the tag includes the information from one page in another. The differences are as follows:
<jsp:include page = ... >: This is like a function call from one jsp to another jsp. It is executed ( the included page is executed  and the generated html content is included in the content of calling jsp) each time the client page is accessed by the client. This approach is useful to for modularizing the web application. If the included file changed then the new content will be included in the output. 

<%@ include file = ... >: In this case the content of the included file is textually embedded in the page that have <%@ include file=".."> directive. In this case in the included file changes, the changed content will not included in the output. This approach is used when the code from one jsp file required to include in multiple jsp files.
Question: What is the difference between <jsp:forward page = ... > and
response.sendRedirect(url),?.
Answer: The <jsp:forward> element forwards the request object containing the client request information from one JSP file to another file. The target file can be an HTML file, another JSP file, or a servlet, as long as it is in the same application context as the forwarding JSP file.
sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page. The  response.sendRedirect kills the session variables.

Question: What are all the different scope values for the <jsp:useBean> tag?
Answer:<jsp:useBean> tag is used to use any java object in the jsp page. Here are the scope values for <jsp:useBean> tag:
a) page
b) request
c) session and
d) application

Exception Handling in JSP:
The exception is normally an object that is thrown at runtime. Exception Handling is the process to handle the runtime errors. There may occur exception any time in your web application. So handling exceptions is a safer side for the web developer. In JSP, there are two ways to perform exception handling:
  • By <error-page> element in web.xml file
  • By errorPage and isErrorPage attributes of page directive
Directory structure of Web application
 

Comments

Popular posts from this blog

Ashtavinayak Temples | 8 Ganpati Temples of Maharashtra | Details Travel Reviews

Ramoji Film City, Hyderabad, India

Pune to Ganpatipule Trip