Why J2EE (Java 2 Enterprise Edition) ?

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

1. Why J2EE (Java 2 enterprise edition) ?

• Rich interaction with a web server via Servlets,which provide objects


representing the elements of the HTTP request and response.
• Built in support for sessions ,both in servlets and EJBs.
• EJBs provide automatic transaction support to EJB operating on the
EJB server.
• Entity beans to represent data as an object.
• Seamless integration with the java data access APIs.
• Flexible template-based output using JSP and XML.
• A clear division between code which deals with presentation,business
logic and data.

2. Why JSP ?
• JSP's provide a much cleaner way of seperation of presentation from
logic.
• It speeds development with the reusable components and tags
embedded in the page.And are simpler to write.
• JSPs are interpreted only once, to Java byte-code, and reinterpreted
only when the file is modified.
• JSP specs is built on top of the Java Servlet API.
• Servlets are supported by almost all major web servers, browsers and
tools.
3. What are JSPs ?
• A JSP page is a page created by the web developer that includes JSP
specific tags, declarations and possibly scriplets, in combination with
other static (HTML or XML) tags.
4. How do JSP’s work ?
• JSP pages use Java scriplets and XML tags to encapsulate the logic
that generates the content for the page.
• The JSP pages are compiled into a Java servlet class and remains in
server memory, so susequent calls to the page have a very fast
response times.
• JSP pages may call JavaBeans components or EJB’s components to
perform the processing on the server.
• A JSP page has a (.jsp) extension, this signals to the web server that
the JSP engine will process elements on this page.
• A JSP page is executed by a JSP engine or container, which is
installed on the web server or an application server.

1
• The page is initilized by invoking jspInit() method just like the
servlets.
• Every time a request comes to the JSP , the container generated
jspService() method is invoked, the request is processed , and the
JSP generates a appropriate response.This response is taken by the
container and passed back to the client.
• When the JSP is destroyed by the server , the jspDestroy() is
invoked to perform any clean up.

5. What does a JSP Page contain ?


• Everything in a JSP can be broken down into two categories:-Element
data that are processed on the server and Template data.
• The Element data are classified in to following categories:-
Directives, Declarations, Scriptlets, Expressions, Standard Actions.
• Directives:-They are used to set global values such as class
declarations, methods to be implemented, output content type,etc.
• They do not produce any output to the client.
• All directives have the scope of the entire JSP file.
• The syntax of a directive is as follows:-
<%@ directivename attribute=”value” attribute=”value” %>
• There are three types of directives:-The page, The include and The
taglib directive.
• PAGE directive defines a number of attributes that affect the whole
page.
• The syntax is as follows:-
<%@ page attributename=”value” %>
• The attributes can be any of the following:-language, import,session,
isThreadSafe, info, isErrorPage, etc…
• E.g:- <%@ page language=”java” import=”java.rmi.*,java.util.*”
session=”true” buffer=”12kb” autoflush=”true” isThreadSafe=”false”
errorPage=”Error.jsp” isErrorPage=”false” %>
• INCLUDE directive notifies the container to include the content of
the resource in the current JSP.
• The syntax is as follows:-
<%@ include file=”filename” %>
• E.g:-<%@ include file=”/examples/myfile.html” %>
• TAGLIB directive allows the page to use custom user defined tags.

2
• It also names a tag library that they are defined in.The engine uses
this tag to find out what to do when it comes across the custom tags
in the JSP.
• URI (Uniform resource identifier) identifies the tag library
descriptor.A tag library descriptor is used to uniquely name the set of
custom tags and tells the container what to do with the specified
tags.
• A tagprefix defines the prefix string in <prefix>:<tagname> that is
used to define a custom tag.(the reserved tag prefixes are jsp, jspx,
java, javax, servlet, sun and sunw).
• The syntax is as follows:-
<%@ taglib uri=”tagLibraryURI” prefix=”tagPrefix” %>
• E.g:- <%@ taglib uri=”http://www.myserver.com/mytags” prefix=”hrishi” %>
<hrishi:processElement>
</hrishi:processElement>
• Scripting Elements are used to include scripting code (normally java
code) within JSP.
• The three types are :-Declarations, Scriptlets, and Expressions.
• A Declaration is a block of Java code in a JSP that is used to define
class-wide variables and methods in the generated class file.
• They are initialized when the JSP page is initialized and anything
defined in a declaration is available throughout the JSP.
• The syntax is as follows:-<%! %>
• E.g:- <%! private int i=4;
Public void myMethod() {
}
%>
• Scriptlets is a block of Java code that is executed at request
processing time.
• A scriptlet is enclosed between <% and %>
• JSP gets compiled into a servlet and all the code between the <% and
%> in the JSP ,gets put in the service() of this servlet,hence it is
processed for every request that the servlet processes.
• An Expression is used to display the output value in the response
stream back to the client.
• An expression is enclosed within <%= and %>.
• E.g:- <%= “This is the output” +i %>.

3
• Actions are specific tags that affect the runtime behaviour of the
JSP and the response sent back to the client.
• The standard action types are :-<jsp:useBean>, <jsp:setProperty>,
<jsp:getProperty>, <jsp:param>, <jsp:include>, <jsp:forward>, <jsp:plugin>
• <jsp:useBean> is used to associate a JavaBean with the JSP.
• The syntax is as follows:-
<jsp:useBean id=”name” scope=”page|request|session|application”
beandetails />
• <jsp:setProperty> is used in conjunction with the useBean action and
sets the value of simple and indexed properties in a bean.
• The syntax is as follows:-
<jsp:setProperty name=”beanname” propertydetails />
• <jsp:getProperty> action is used to access the properties of a bean.
• The syntax is as follows:-
<jsp:getProperty name=”name” property=”propertyname” />
• <jsp:param> action is used to provide other tags with additional
information in the form of name/value pairs.This action is used in
conjunction with the jsp:include, jsp:forward, jsp:plugin.
• The syntax is as follows:-
<jsp:param name=”paramname” value=”paramvalue” />
• <jsp:include> action allows a static or dynamic resource to be
included in the current JSP at request time.
• It can have one or more jsp:param tags in its body.
• The syntax is as follows:-
<jsp:include page=”urlspec” flush=”true”>
<jsp:param name=”paramname” value=”paramvalue” />
</jsp:include>
• <jsp:forward> action allows the request to be forwarded to another
JSP, a servlet or a static resource.
• The resource to which the request is being forwarded must be in the
same context as the JSP dispatching the request.
• The syntax is as follows:-
<jsp:forward page=”url” />
<jsp:param name=”paramname” value=”paramvalue”>
</jsp:forward>
• <jsp:plugin> action is used to generate client browser specific HTML
tags.
• The syntax is as follows:-

4
<jsp:plugin type=”bean|applet” code=”objectname”
codebase=”objectcodebase” align=”alignment” height=”height”
hspace=”hspace” vspace=”vspace” width=”width” nspluginurl=”url”
iepluginurl=”url” >
<jsp:param name=”paramname” value=”paramvalue” />
<jsp:fallback>Alternate text to display </jsp:fallback>
</jsp:plugin>

6. What are CONTEXTS and various SCOPES ?


• JSP is designed to be portable between different servers.
• The context provides an abstraction and bridging of the gap between
server-specific implementations and portable JSP code.
• A context provides an invisible container for resources and an
interface for them to communicate with their enviroment.
• For eg a servlet executes in a context.Everything that a servlet needs
to know about its server can be extracted from its context and
everything that the server wants to communicate to the servlet goes
through the context.
• Everything in a JSP is associated with a context and every context
has a scope.
• The PAGE_SCOPE :-the object reference is discarded upon
completion of the current service().
• The REQUEST_SCOPE :-the object reference is available as long as
the HttpRequest object exixts, even if the request is passed/chained
to different pages.
• The SESSION_SCOPE :-the object is distinct for every client amd is
available as long as the clients session is valid.
• The APPLICATION_SCOPE :-This is the most persistent.This is not
unique to individual clients and, consequently ,all clients access the
same object.

7. What are the types of ERRORS and EXCEPTIONS ?


• There are two primary types of exceptions you can run into while
processing with JSP pages.Translation time errors and Run time
exceptions.
• Translation errors occur during page compilation,this results in an
Internal Servlet Eror(error code 500).

5
• Common translation time errors are caused by missing semicolons,
forgetting to import a package, or using the wrong JSP directives.
• Where as Exceptions occur once the page is compiled.Exceptions are
dealt with within the JSP using the standard Java exception-handling
methods.
• The error page can be used to display a more user-friendly error
message.(using the isErrorPage, errorPage attributes).

8. What is need for tag extension mechanism ?


• Web application often want to provide multiple views on the same data
to different channels(WML,HTML).It is important in these
applications to separate the view from the controller so that the same
controller can create different views of the same data.
• You can customise u r own tags.

9. What is a taglib tag ?


• Every tag u define should be declared in the directive with the
extension (.tld).
• The taglib directive references an XML document that defines the
tags operation.
• We first define a tag library using the <taglib> tag.Then for each
custom tag we need a <tag> entry.
• Inside the <tag> entry a name(which we use in the JSP).
• A tagclass that maps the name of the fully-qualified Java class, which
implements the tag interface.
• A bodycontent that specifies the content type of the tag.
• Optional information.
• E.g:-
<?xml version=”1.0” encoding=”ISO-8859-1” ?>
<!DOCTYPE taglib
PUBLIC “-//sun microsystems………..” http://java.sun.com/j2ee/dtds/web-
jsplibrary_1_1.dtd>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>name</shortname>
<uri>http://www.wrox.com</uri>
<info>information</info>

6
<tag>
<name>name</name>
<tagclass>class name of tag</tagclass>
<teiclass>class name of tagExtraInfo</teiclass>
<bodycontent>empty|jsp|tagdependent</bodycontent>
<info>…</info>
</tag>
</taglib>
• The basic interface that a custom tag must implement is the Tag
interface.
• The TagSupport class provides a default inplementation, so u only
override the methods doStartTag() and doEndTag().
• The name of the tag ,class name,optional TagExtraInfo
implementation,body content type,attributes,and other desciptive
information is stored in a <tag> entry in the taglib.
• The TEI(TagExtraInfo) class provides a way to deal with the
complexities of setting a variable for use within the tag or in the
page.

10.What is the life cycle of a “Tag with no body” ?


• The doStartTag() method is evaluated once the tag object has been
initialized with any attributes and page context information.
• The default implementation in the TagSupport class is to return
SKIP_BODY,so the body of the tag is not evaluated.
• If u want to evaluate the body of the tag then return
EVAL_BODY_INCLUDE from this method.
• The doEndTag() method is called when the tag evaluation is done.Its
default return is EVAL_PAGE which allows it to continue with rest of
the page.
• You can halt the JSPs output ,because of an error then return
SKIP_PAGE.
• The void release() method is called at the end of every tag.

11.What is the life cycle of a “Tag with a body” ?


• We use the BodyTag interface.
• For body tags, the doStartTag() returns EVAL_BODY_TAG to
evaluate the body,
• Then the doInitBody() method is invoked.

7
• The body between the tags is written to the bodycontent stream,then
the doAfterBody() method is called which returns a default
SKIP_BODY,so that doEndTag() is called.
• Then atlast the void release() method is called.

You might also like