Java Server Pages

· JSP Directives:

  1. Page Directive: Define & manipulate a number of important page dependent attributes that affect the whole JSP, & communications these attributes to the JSP container.
     <%@ page  [ language = "java" ]
     [ extends = "package.class" ]
     [ import = "package.*" ]
     [ session = "true | false" ]
     [ buffer = "none | 8kb" ]
     [ autoFlush = "true | false" ]
     [ isThreadSafe = "true | false" ]
     [ info = "text" ]
     [ errorPage = "relative URL" ]
     [ contentType ="text/html; charset=ISO-8859-1" ]
     [ isErrorPage = "true | false" ]
     [ pageEncoding = "ISO-8859-1" ]
%>
  1. Include Directive: Instructs container to include the content of the resource in the current JSP.
<%@ include file = "relative URL" %>
  1. Taglib Directive: Defines a tag library and prefix for the custom tags used in the JSP page.
<%@ taglib uri=" URIToTagLibrary " 
                 prefix=" tagPrefix " %>

where prefix = jsp, jspx, java, javax, sun,servlet,sunw

· Scripting Elements:

  1. Declarations (between <%! and %> tags) contain one or more variable or method declarations that end or are separated by semicolons:
<%! int i = 0; %>
<%! int a, b; double c; %>
  1. Expressions (between <%= and %> tags) can contain any language expression that is valid in the page scripting language, but without a semicolon:
<%= Math.sqrt(2) %>
<%= items[i] %>
<%= a + b + c %>
<%= new java.util.Date() %>
  1. Scriptlets (between <% and %> tags) allow you to write any number of valid scripting language statements, like this:
<%       String name = null;
     if (request.getParameter("name") == null) {
%>

· Standard Actions: Tags that affect the runtime behaviour of the JSP & response sent back to the client.

         <jsp:include   page="relative URL" 
                       flush="true | false" />
        <jsp:useBean  id="beanInstanceName"  
        scope=" page |request| session| application" />
        <jsp:setProperty   name="bean Instance name" 
                       property=" * " />
        <jsp:getProperty   name="bean Instance name" 
                       property=" * " />
        <jsp:Param   name="param name" 
                       value=" param Value " />
        <jsp:forward page="relative URL"  />
                       or
        <jsp:forward page="relative URL" >
               <jsp:param  name="param Name" 
                               value="param Value" />
        </jsp:forward>
        <jsp: plugin   type=" bean| applet" 
               code=" className " codebase=" Dir "
               [ name=" instanceName " ] 
               [ archive=" URIToArchive, ... " ] 
               [ align=" bottom |top| middle| left| right" ]
               [ height=" displayPixels " ] 
               [ width=" displayPixels " ] 
               [ hspace=" leftRightPixels " ] 
               [ vspace=" topBottomPixels "]
               [ jreversion=" JREVersionNumber | 1.1 " ] 
               [ nspluginurl=" URLToPlugin " ] 
               [ iepluginurl=" URLToPlugin "] >
        </ jsp: plugin>

· Implicit Objects: based on Servlet API.

Objects

Type

Scope

Useful Methods

request

Subclass of javax.servlet.ServletRequest

Request

getParameter, getParameterNames, getParameter Values

response

Subclass of javax.servlet.ServletResponse

Page

Not typically used by JSP page authors

pageContext

javax.servlet.jsp.PageContext

Page

findAttribute, getAttributeScope, getAttributeNamesinScope

session

javax.servlet.http.HttpSession

Session

getID, getValue, getValueNames, putValue

application

javax.servlet.ServletContext

Application

getMimeType, getRealPath

out

javax.servlet.jsp.JspWriter

Page

clear, clearBuffer, flush, getBufferSize, getRemaining

config

javax.servlet.ServletConfig

Page

getInitParameter, getInitParameterNames

Page

java.lang.Object

Page

Not typically used by JSP page authors.

exception

java.lang.Throwable

Page

getMessage, getLocalizedMessage, printStackTrac

· Difference between <%@ include file=”abc.jsp” %>
& <jsp:include page=”abc.jsp” %>

The <%@include file=”abc.jsp”%> directive acts like C “#include”, pulling in the text of the included file and compiling it as if it were part of the including file. The included file can be any type (including HTML or text).

The <jsp:include page=”abc.jsp”> tag compiles the file as a separate JSP file, and embeds a call to it in the compiled JSP.

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

· Explain the life-cycle mehtods 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 inturn extends the Servlet interface of the javax.servlet package. the generated servlet class thus implements all the methods of the these three interfaces. The JspPage interface declares only two mehtods – 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 interfaec specifically for the JSp pages serving HTTP requests. This interface declares one method _jspService().
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.

· What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?
request.getRequestDispatcher(path): In order to create it we need to give the relative path of the resource,
context.getRequestDispatcher(path): In order to create it we need to give the absolute path of the resource.

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

· JSP Directives: Page Directive: Define & manipulate a number of important page dependent attributes that affect the whole JSP, & communications these attributes to the JSP container. <%@ page [ language = “java” ] [ extends = “package.class” ] [ import = “package.*” ] [ session = “true | false” ] [ buffer =…

Leave a Reply

Your email address will not be published. Required fields are marked *