Books Related to Java, Core Java, Jquery, Web Services, SCJP, Spring, Hibernate and J2ME

Sunday, January 25, 2009

JSP Action Tags

This is the another type of tag, it is used to transfer the control between

pages and is also used to enable the use of server side JavaBeans. Instead of
using Java code, the programmer uses special JSP action tags to
either link to a Java Bean set its properties, or get its
properties.

Syntax of Action Tag: "<jsp:action attributes />"

There are so many action tags available for Java Server Pages.
The most commonly used action tags are three of them that are:


  1. include

  2. forward

  3. useBean

1. Include Action Tag:- It has the same concept as that of the

include directive. The include tag
is used to include either static or dynamic content, wherever required.

Syntax of include action Tag: <jsp:include page="{relativeURL | <%=

expression %>}" flush="true" />


If the file is dynamic one can use the syntax as below:

<jsp:include page="{relativeURL | <%= expression %>}" flush="true" />

<jsp:param name="parameterName" value="{parameterValue
| <%= expression %>}" />

</jsp:include>

For example: An example to include a static file jsp-directives.html

in the include action tag:

<jsp:include page="jsp-directives.html"

flush="true"/>

Let us see an example of dynamic file inclusion in the include action tag

which is done as below:

<jsp:include page="test/jsp-directives.html.jsp">

<jsp:include name="username" value="sandeep" />

</jsp:include>


2. Forward Action Tag:- The forward action tag is used to transfer
control to a static or dynamic resource. The static or dynamic resource to which
control has to be transferred is represented as a URL. The user can have the
target file as an HTML file, another JSP file, or a servlet. Any of the above is
permitted.

Syntax of forward action Tag: &nbsp;

<jsp:forward page="{relativeURL | <%= expression %>}" />


Now another representation for forward action Tag is as:

<jsp:forward page ="{relativeURL | <%= expression %>}" />

<jsp:param name ="parameterName" value="parameterValue"
| <%= expression %>}" />

</jsp:forward>

3. useBean Action Tag:- The useBean action tag is the most commonly

used tag because of its powerful features. It allows a JSP to create an instance
or receive an instance of a Java Bean. It is used for creating or instantiating
a bean with a specific name and scope. This is first performed by locating the
instance of the bean.

Syntax of useBean action Tag is:

<jsp:useBean id="beanInstanceName" scope="page|request|session|application"

{

class="package.class" | type="package.class"
|

class="package.class" type="package.class" |

beanName="{package.class | <%=
expression %>}"

type="package.class"

}

{ /> |

> other elements

</jsp:useBean>

}


There are the several attributes available in useBean Action Tag, that is:


  • id: This refers to a variable that is case sensitive, identifying
    the bean within scope attribute defined.

  • scope: This refers to the scope of the existence of bean. The
    default scope is page.

  • class: The class name is mentioned and using this bean is
    Instantiated. The data type to which bean must be assigned is mentioned in
    type.

  • beanName: When this is specified, the bean is instantiated by the
    java.beans.Beans.instantiate method.



JSP Directives

In this section, you will study about the Directive tag. It allows the JSP engine to processes the page. By using the directive tag, user can import different packages, handles the errors, or session information of JSP page. These are classified in three categories:

  1. Page
  2. Include
  3. Tag Lib

Syntax and usage of directive tag :

1. Page Directives: There are the many attributes are available for page directives which is used to give the special processing information to the JSP Engine that are:

  • language
  • extends
  • import
  • session
  • buffer
  • autoFlush
  • isThreadSafe
  • info
  • errorPage
  • IsErrorPage
  • contentType

language:- This attribute is used to denote the language used by a file. Language denotes the scripting language used in script lets, declarations, and expressions in the JSP page and any included files.

Syntax of language attribute is: <%@ page language = "java" %>

extends:- This is used to signify the Super class of the Java class used by the JSP engine for the translated Servlet.

Syntax of extends attribute is: <%@ page extends = "package.class"%>

import:- The import attribute is used to import all the classes in a java package into the current JSP page. With this facility, the JSP page can use other java classes.

Syntax of import attribute is: <%@ page import = "java.util.*" %>

session:- The session attribute, when set to true, sets the page to make use of sessions. by default, the session attribute value is set to true.

Syntax of session attribute is: <%@ page session="true|false" %>

buffer:- It controls the use of buffered output for a JSP page then the buffer attribute can be made use of for achieving this.

Syntax of buffer attribute is: <%@ page buffer = "none|8kb|sizekb" %>

autoFlush:- This attribute is used to specify whether automatically flush out the output buffer when it is full or not.

Syntax of autoFlush attribute is: <%@ page autoFlush = "true|false" %>

isThreadSafe:- This attribute is used to set whether the generated Servlet handles multiple requests or single requests, depending on the set value of true or false. isThreadSafe attribute is used to set and ensure whether thread safety is implemented in the JSP page.

Syntax of isThreadSafe attribute is: <%@ page isThreadSafe="true|false" %>

info:- Programmers make use of info attribute to place the information or documentation for a page. Details such as: author, version, copyright and date are placed in this attribute. This is actually text string that is written as input text in the compiled JSP page.

Syntax of info attribute is: <%@ page info = "text" %>

errorPage:- If the programmer wants to place errors in a different page then the URL to the error page can be mentioned in this attribute as errorPage.

Syntax of errorPage attribute is: <%@ page errorPage = "relativeURL" %>

isErrorPage:- This attribute is used to specify whether or not a JSP page displays an error page by setting the value as true or false. By default, the value is set to false, meaning that the user cannot make use of the exception object in the JSP page. If the value is set to true, then it means that the user can make use of the exception object in the JSP page.

Syntax of isErrorPage attribute is: <%@ page isErrorPage="true|false" %>

contentType:- This attribute is used to set the mime type and character set of the JSP. The user can make use of any MIME type or character set valid for the JSP container.

Syntax of contentType attribute is: <%@ page contentType="mimeType [; charset=characterSet]" %>

2. Include Directives:- This is a type of directive tag. If a programmer wants to include contents of a file inside another, then the Include directive is used. The file included can be either static ( HTML file) or dynamic (i.e., another tag file). The Include directive is used to include the contents of other file in the current tag file.

Syntax of Include directive is: <%@ include file = "xxx.html/xx.tagf"%>

For example: The user includes a dynamic file in the include directive: <%@ include file="javasks.tagf" %>.

3. Tag Lib Directives:- A user can customize actions from their tag file using the Tag Lib directive. The Tag Lib directive is a collection of custom tags.

The syntax of Tag Lib directive is: <%@ taglib uri = "tag library URI" prefix = "tag Prefix" %>

scriptlet tag:- This tag is a type tag used for inserting Java Code into JSP. Notation of the Scriptlet tag is: <% %>

Reference Sit:- http://www.exforsys.com/tutorials/jsp/jsp-directive-tag.html.

Friday, January 23, 2009

Life Cycle of JSP

JSP technology enables us to create web based application. It sends its request as a Servlet. Therefore, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology.

A Servlet handles the request that is mapped to a JSP page and checks whether the JSP page's servlet is older than the JSP page. Then it translates the JSP page into a servlet class and compiles the class.

JSP Life Cycle has three important methods that is:

  1. init():- This method is called when the instance is created and it is called only once during JSP life cycle. It is called for the Servlet instance initialization.
  2. service:- This method is called for every request of this JSP during its life cycle. It passes the request and the response objects. jspService() cannot be overridden.
  3. destroy():- This method is called when this JSP is destroyed and will not be available for future requests.

Phases of JSP Life Cycle:

  1. Translation:
    The JSP source file generates a java servlet file. Generated servlet implements the interface javax.servlet.jsp.HttpJspPage. The interface HttpJspPage extends the interface JspPage. This interface JspPage extends the interface javax.servlet.Servlet.
  2. Compilation:
    The generated java servlet file is compiled into a java servlet class. The generated servlet class thus implements all the methods of the above said three (javax.servlet.jsp.HttpJspPage, JspPage, javax.servlet.Servlet) interfaces.
  3. Class Loading:
    The java servlet class that was compiled from the JSP source is loaded into the container.
  4. Instance Creation:
    An instance is created for the loaded servlet class. The interface JspPage contains jspInit() and jspDestroy(). The JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP requests and this interface contains _jspService().

JavaServer Pages technology

JavaServer Pages (JSP) technology provides a simplified, fast way to create web pages that display dynamically-generated content. The JSP specification, developed through an industry-wide initiative led by Sun Microsystems, defines the interaction between the server and the JSP page, and describes the format and syntax of the page.

Wednesday, January 21, 2009

JSP Scriptlet Question and Answer

  1. I have created two buttons in a JSP page.i want to go to one page when button1 is clicked and to other page when button2 is clicked..how can i do this?..Plz help me..
  2. Write the JSP command which includes sub_page.jsp and passing in parameters as below: Parameter: ?age? Value: ?33? Parameter: ?place? Value: ?petaling jaya?
  3. I m sending my data frm one page. The second page process it & saves to database and display message like DATA SAVED on same page(second). If I refresh the browser the same query re-execute & again same data store. How Can I check this(I meanwithout using sendredirect to another page)
  4. What is the max limit of parameters that can be passed from Form.
  5. How can I restrict the user from Clicking of Back button in any browser?
  6. Why HTML is called as a language?
  7. What is the widely used web programming technology? what is the advantages of JSP among other technologies?
  8. I have to view(in a JSP Page)the rows in a database which contains some thousands of rows.But in a page i have to view only 20 rows and by pressing the hyperlinks in the page(for an example :page1, page2)i have to view the rest of the records. Each page should contain 20 records only. Kindly give me an idea or some sample coddings to design the above JSP page.
  9. How to compile the JSP pages manually in all applications and web servers
  10. How are the implicit object made available in the jsp file? Where are they defined?
  11. Can one JSP or Servlet extend another Servlet or JSP
  12. suppose we have 1000 rows in the database i need to retrive 200 rows per page, then which approach is better either struts based or non struts based it is an interview question
  13. What is the difference between declaring the variables in the scriptlet tags and in the declaration tags?
  14. What is the differene between include directive and include action?
  15. What is difference between JSP 1.0 model and JSP 2.0 model
  16. What is jsp:usebean. What are the scope attributes & difference between these attributes
  17. How can we move from one JSP page to another(mean using what tecnique?)
  18. How can you include a static file in JSP page. is it possible in html style sheet. which one is better jsp's include directive ("<%@include="filename"%>) or html's style sheet.
  19. Can we implements interface or extends class in JSP?
  20. How can we move from one JSP page to another(mean using what tecnique?)

Important JSP Interview Question with Solution

  1. What is JSP?
  2. What are the lifecycle of JSP?
  3. What is a translation unit?
  4. How is JSP used in the MVC model
  5. What are context initialization parameters
  6. What is a output comment
  7. What is a Hidden Comment
  8. What is a Expression
  9. What is a Declaration
  10. What is a Scriptlet
  11. What are the implicit objects
  12. What is the difference between forward and sendRedirect
  13. What are the different scope values for the
  14. Why are JSP pages the preferred API for creating a web-based client program
  15. Is JSP technology extensible
  16. What is difference between custom JSP tags and beans
  17. How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?
  18. How does JSP handle run-time exceptions
  19. How do I prevent the output of my JSP or Servlet pages from being cached by the browser
  20. How do I use comments within a JSP page
  21. Response has already been commited error. What does it mean
  22. How do I use a scriptlet to initialize a newly instantiated bean
  23. How can I enable session tracking for JSP pages if the browser has disabled cookies
  24. How can I declare methods within my JSP page
  25. Is there a way I can set the inactivity lease period on a per-session basis
  26. How can I set a cookie and delete a cookie from within a JSP page
  27. How does a servlet communicate with a JSP page
  28. How do I have the JSP-generated servlet subclass my own custom servlet class, instead of the default
  29. How can I prevent the word "null" from appearing in my HTML input text fields when I populate them with a resultset that has null values
  30. How can I get to print the stacktrace for an exception occuring within my JSP page
  31. How do you pass an InitParameter to a JSP
  32. How can my JSP page communicate with an EJB Session Bean
  33. Can we implement an interface in a JSP
  34. What is the difference between ServletContext and PageContext
  35. What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()
  36. How to pass information from JSP to included JSP
  37. What is the difference between directive include and jsp include
  38. What is the difference between RequestDispatcher and sendRedirect
  39. How do I mix JSP and SSI #include