Java Viva 2
Java Viva 2
______________________________________________________________________________
______________________________
This method also can throw a ServletException. The servlet container cannot
place the Servlet into service if the init method throws a ServletException or the
method does not return within a time period defined by the web server.
(3) Unloading:
The Servlet container unloads a Servlet class by invoking the Servlet’s destroy()
method. Typically, this method is invoked when the Servlet container is
stopping a web application which contains the Servlet. The method runs only
once during the lifetime of the Servlet and signals the end of the Servlet. After
a Servlet’s destroy() method is invoked, the Servlet container unloads the
Servlet, and the JVM eventually performs garbage collection on the memory
resources associated with the Servlet.
______________________________________________________________________________
______________________________
Packages
______________________________________________________________________________
______________________________
ServletConfig ServletContext
(1) ServletConfig is one for each Servlet or JSP. (1) ServletContext is one for each w
(3) Code for accessing Servlet init parameter is: (3) Code for accessing Servlet con
getServletConfig().getInitParameter(“Email”) getServletContext().getInitParame
______________________________________________________________________________
______________________________
9. Why is HTTP stateless? How is statelessness of HTTP overcome to run e-
Commerce applications?
Answer: The Hypertext Transfer Protocol (HTTP) is the protocol that web server
and web browser uses to communicate with each other. HTTP connections are
initiated by a client browser that sends an HTTP request. The web server then
responds with an HTTP response and closes the connection. If the same client
requests another resource from the server, it must open another HTTP
connection to the server. Server always closes the connection as soon as it
sends the response, whether or not the browser user needs some other
resource from the server. That is why HTTP is said to be stateless protocol.
Being stateless has huge implications. HTTP treats each request as a request
from new user. But it is not expected when we are doing any type of
transactions or any other related work where persistence of information is
necessary. As a result, it is difficult to implement web sites that react
intelligently to the user input.
In order to have stateful communication for running e-Commerce applications
on the web, the web site developers must make some necessary arrangements
for the server to send the state (or some representative of the state) to the
client, and for the client to send it back again next time. This is called session
management which is being supported by Servlet/JSP. There are four ways this
happens in HTTP. One is using session objects, in which case the state is sent
and returned in HTTP headers. The second technique is called cookies which
are small bits of textual information that a web server sends to a browser and
that browser returns the cookie when it visits the same site again. In cookie, the
information is stored in the form of a name-value pair. The third one is URL
rewriting, in which case the state is sent as part of the response and returned
as part of the request URI. The fourth is hidden form fields, in which the state is
sent to the client as part of the response, and returned to the server as part of
a form’s data (which can be in the request URI or the POST body, depending on
the HTML form’s method).
______________________________________________________________________________
______________________________
______________________________________________________________________________
______________________________
Refers to t
javax.servlet.http.HttpServletRequest to the pag
request
Used for s
javax.servlet.http.HttpServletResponse to the clie
response
Refers to
javax.servlet.jsp.JspWriter for the pag
out
javax.servlet.http.HttpSession Refers to t
session
Refers to
javax.servlet.ServletContext environm
application
Refers t
javax.servlet.ServletConfig configurat
config
Refers
javax.servlet.jsp.PageContext environm
pageContext
Refers to
javax.servlet.jsp.HttpJspPage instance
page
Used for
java.lang.Throwable purposes
exception
______________________________________________________________________________
______________________________
1. The user goes to a web site made using JSP. The user goes to a JSP
page (ending with .jsp). The web browser makes the request via
the Internet.
2. The JSP request gets sent to the Web server.
3. The Web server recognizes that the file required is special (.jsp),
therefore passes the JSP file to the JSP Servlet Engine.
4. If the JSP file has been called the first time, the JSP file is parsed,
otherwise go to step 7.
5. The next step is to generate a special Servlet from the JSP file. The
entire HTML required is converted to println statements.
6. The Servlet source code is compiled into a class file.
7. The Servlet is instantiated, calling the init and service methods.
8. HTML from the Servlet output is sent via the Internet.
9. HTML results are displayed on the user’s web browser.
______________________________________________________________________________
______________________________
18. What are the different types of JSP directives? Explain each with
example.
Answer: Directives provide general information about the JSP page to the JSP
engine. A directive tag always starts with <%@ and ends with %>.
There are 3 types of directives: page, include, and taglib.
The general syntax for the 3 directives is:
In the above shown syntax, the attribute-list represents one or more attribute
value-pairs that are specific to the directive. Some important points that are
needed to be remembered about the syntax of the directive are as follows:
• The tag names, their attributes, and their values are all case
sensitive.
• The value must be enclosed within a pair of single or double
quotes.
• A pair of single quotes is equivalent to a pair of double quotes.
• There must be no space between the equals sign (=) and the value.
A page directive informs the JSP engine about the overall properties of a JSP
page. For example, the following page directives inform the JSP engine that
Java will be used as scripting language in our JSP page:
<%@ page language=”java” %>
An include directive tells the JSP engine to include the contents of another file
(HTML, JSP, etc) into the current file. For example:
<%@ include file=”test.html” %> or <%@ include file=”test.jsp”
%>
A taglib directive is used to associate a prefix with a tag library. For example:
<%@ taglib prefix=”test” uri=”taglib.tld” %>
______________________________________________________________________________
______________________________
______________________________________________________________________________
______________________________
23. What are the advantages of Servlets / JSPs over other technologies?
Answer: Servlets / JSPs have the following advantages over other technologies
–
• Performance. The performance of Servlets is superior to CGI
because there is no process creation for each client request.
Instead, each request is handled by the Servlet container process.
After a Servlet is finished processing a request, it stays resident in
memory, waiting for another request.
• Portability. Similar to other Java technologies, Servlet applications
are portable. We can move them to other operating systems
without serious hassles.
• Rapid development cycle. As a Java technology, Servlets have
access to the rich Java library, which helps speed up the
development process.
• Robustness. Servlets are managed by the Java Virtual Machine.
As such, we don’t need to worry about memory leak or garbage
collection, which helps us write robust applications.
• Widespread acceptance. Java is a widely accepted technology.
This means that numerous vendors work on Java-based
technologies. One of the advantages of this widespread
acceptance is that we can easily find and purchase components
that suit our needs, which saves precious development time.
• Secure. Servlets are server side components, so it inherits the
security provided by the web server. Servlets are also benefited
with Java Security Manager.
• Extensibility. The Servlet API is designed in such a way that it can
be easily extensible. As it stands today, the Servlet API supports
HTTP Servlets, but in later date it can be extended for another type
of Servlets.