EARN 400$ PER WEEK

Custom Search

Monday, November 8, 2010

Important concepts on Java Server Pages (JSP):

Java Server Page is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages. JSP’s are secure, platform-independent, and best of all; make use of Java as a server-side scripting language.

JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.

JSP TAGS:

JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries

Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. To use custom JSP tags, you need to define three separate components:

1. the tag handler class that defines the tag's behavior

2. the tag library descriptor file that maps the XML element names to the tag implementations

3. the JSP file that uses the tag library

1. Directive tags:

· Page directive: simple one for import statements.

· Include directive: to include specified file in jsp page.

· Taglib directive: used to create custom tags.

2. Scriplet tags:

· Scriplet tag: to specify java code with in jsp page.

· Expression tag: to evaluate an expression.

· Declaration tag: to declare variables. Etc.

3. Action tags:

· forward action: forward from one jsp page to another jsp

Page similar to “request dispatcher class in servlets”

· Include action: very much similar to include directive

except the imp. Difference. It’s for dynamic content where

as that is for static content.

· Use bean action: working with beans using jsp page.


Implicit Objects:

Certain objects that is 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:

  1. Request : same as HttpServletRequest object.
  2. Response : same as HttpServletResponse object.
  3. Page : same as java.lang.Object.
  4. Config : same as ServletConfig.
  5. Context : same as servletContext.
  6. Out : same as printWriter object in servlets.
  7. Session: Http: same as Session obj.
  8. Page context: like page Context.
  9. Exception : java.lang.exception.

FAQ’s on JSP:

1. Difference between forward and sendRedirect?

When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completely with in the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.

2. How does JSP handle runtime exceptions?

Using errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting of a JSP.

3. How do you delete a Cookie within a JSP?

         Cookie mycook = new Cookie (\"name\", \"value\");
         response.addCookie (mycook);
         Cookie killmycook = new Cookie (\"mycook\",\"value\");
         killmycook.setMaxAge (0);
         killmycook.setPath (\"/\");
         killmycook.addCookie (killmycook);

4. How to pass information from JSP to included JSP?

Using <%jsp: param> tag.

5. What is the difference between directive include and jsp include?

<%@ include>: Used to include static resources during translation time.

JSP include: Used to include dynamic content or static content during runtime.

6. What are the different scope values for the ?

The different scope values for are

1. Page
2. Request
3. Session
4. Application

7. Explain the life-cycle methods in JSP?

The generated servlet class for a JSP page implements the HttpJspPage interface of the javax.servlet.jsp package.

The HttpJspPage interface extends the JspPage interface which in turn extends the Servlet interface of the javax.servlet package. The generated servlet class thus implements all the methods of these three interfaces. The JspPage interface declares only two methods – jspInit () and jspDestroy () that must be implemented by all JSP pages regardless of the client-server protocol.

However the JSP specification has provided the HttpJspPage interface specifically for the JSP pages serving HTTP requests. This interface declares one method _jspService ().


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

8. What is a Scriptlet?

A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language. Within scriptlet tags, you can

1. Declare variables or methods to use later in the file
2. Write expressions valid in the page scripting language
3. Use any of the JSP implicit objects or any object declared with a tag.
You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet.

Scriptlets are executed at request time, when the JSP engine processes the client request. If the scriptlet produces output, the output is stored in the out object, from which you can display it.

9. What is an output comment?

A comment that is sent to the client in the viewable page source. The JSP engine handles an output comment as uninterrupted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page source from your Web browser.

10. What is a Hidden Comment?

A comment that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags. A hidden comment is not sent to the client, either in the displayed JSP page or the HTML page source. The hidden comment is useful when you want to hide or "comment out" part of your JSP page.

You can use any characters in the body of the comment except the closing -- %> combination. If you need to use -- %> in your comment, you can escape it by typing -- %\>.
JSP Syntax
<%-- comment -- %>

Examples
<%@ page language="java" %>

<%-- This comment will not be visible to the client in the page source --%>

No comments: