Why J2EE (Java 2 Enterprise Edition) ?
Why J2EE (Java 2 Enterprise Edition) ?
Why J2EE (Java 2 Enterprise Edition) ?
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.
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>
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).
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.
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.