Java Server Pages
by krishna
· 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 = "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" ]
%>
- Include Directive: Instructs container to include the content of the resource in the current JSP.
<%@ include file = "relative URL" %>
- 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:
- 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; %>
- 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() %>
- 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 =…
Recent Comments
Archives
- August 2025
- July 2025
- June 2025
- May 2025
- April 2025
- March 2025
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- February 2012
- January 2012
- December 2011
- October 2011
- August 2011
- July 2011
- May 2011
- January 2011
- November 2010
- October 2010
- September 2010
- July 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- August 2008
- July 2008
- June 2008
- December 2007
- April 2007
- January 2007