Académique Documents
Professionnel Documents
Culture Documents
*Short-Notes
3. Hidden form fields. HTML forms have an entry that looks like the following: <INPUT
TYPE="HIDDEN" NAME="session" VALUE="...">. This means that, when the form is
submitted, the specified name and value are included in the GET or POST data. This can be
used to store information about the session. However, it has the major disadvantage that it
only works if every page is dynamically generated, since the whole point is that each
session has a unique identifier.
Servlets provide an outstanding technical solution: the HttpSession API. This is a high-level
interface built on top of cookies or URL-rewriting. In fact, on many servers, they use cookies if
the browser supports them, but automatically revert to URL-rewriting when cookies are
unsupported or explicitly disabled. But the servlet author doesn't need to bother with many of the
details, doesn't have to explicitly manipulate cookies or information appended to the URL, and is
automatically given a convenient place to store data that is associated with each session.
2.1 Looking up the HttpSession object associated with the current request.
This is done by calling the getSession method of HttpServletRequest. If this returns null,
you can create a new session, but this is so commonly done that there is an option to
automatically create a new session if there isn't one already. Just pass true to getSession. Thus,
your first step usually looks like this:
HttpSession session = request.getSession(true);
(ShoppingCart)session.getValue("previousItems");
if (previousItems != null) {
doSomethingWith(previousItems);
} else {
previousItems = new ShoppingCart(...);
doSomethingElseWith(previousItems);
}
In most cases, you have a specific attribute name in mind, and want to find the value (if any)
already associated with it. However, you can also discover all the attribute names in a given
session by calling getValueNames, which returns a String array. In version 2.2, use
getAttributeNames, which has a better name and which is more consistent in that it returns an
Enumeration, just like the getHeaders and getParameterNames methods of
HttpServletRequest.
Although the data that was explicitly associated with a session is the part you care most about,
there are some other pieces of information that are sometimes useful as well.
getId. This method returns the unique identifier generated for each session. It is
sometimes used as the key name when there is only a single value associated with a
session, or when logging information about previous sessions.
isNew. This returns true if the client (browser) has never seen the session, usually
because it was just created rather than being referenced by an incoming client request. It
returns false for preexisting sessions.
getCreationTime. This returns the time, in milliseconds since the epoch, at which the
session was made. To get a value useful for printing out, pass the value to the Date
constructor or the setTimeInMillis method of GregorianCalendar.
getLastAccessedTime. This returns the time, in milliseconds since the epoch, at which
the session was last sent from the client.
getMaxInactiveInterval. This returns the amount of time, in seconds, that a session
should go without access before being automatically invalidated. A negative value
indicates that the session should never timeout.
// You still have to do putValue, not just modify the cart, since
// the cart may be new and thus not already stored in the session.
session.putValue("previousItems", previousItems);
Initializing a Servlet
When a server loads a servlet, the server runs the servlet's init method. Initialization completes
before client requests are handled and before the servlet is destroyed.
Even though most servlets are run in multi-threaded servers, servlets have no concurrency issues
during servlet initialization. The server calls the init method once, when the server loads the
servlet, and will not call the init method again unless the server is reloading the servlet. The
server can not reload a servlet until after the server has destroyed the servlet by calling the
destroy method.
Destroying a Servlet
Servlets run until the server are destroys them, for example, at the request of a system
administrator. When a server destroys a servlet, the server runs the servlet's destroy method.
The method is run once; the server will not run that servlet again until after the server reloads
and reinitializes the servlet.
When the destroy method runs, another thread might be running a service request. The
Handling Service Threads at Servlet Termination lesson shows you how to provide a clean
shutdown when there could be long-running threads still running service requests.
java.sql
package.
The core API is also composed of a set of classes apart from interfaces, and these classes and
interfaces work together and are linked as shown in the diagram below:
All the statements in the JDBC interface eventually execute SQL to produce a ResultSet, and hence
the Statement class is a parent to all the other Statement classes. And all the statements are
executed using the underlying Connection to the database.
A Statement object is used to send SQL statements to a database. There are actually three kinds of
Statement objects, all of which act as containers for executing SQL statements on a given
connection: Statement, PreparedStatement, which inherits from Statement, and
CallableStatement, which inherits from PreparedStatement. They are specialized for sending
particular types of SQL statements: A Statement object is used to execute a simple SQL statement
with no parameters, a PreparedStatement object is used to execute a pre-compiled SQL statement
with or without IN parameters, and a CallableStatement object is used to execute a call to a
database stored procedure. We will start out by discussing the Statement object and then move onto
the more advanced PreparedStatement and CallableStatement statement objects.
The following diagram depicts the role of a class called DriverManager in a typical JDBC application.
The DriverManager acts as the bridge between a Java application and the backend database. When
the getConnection() method of this class is called, the DriverManager decides which JDBC driver
to use for connecting to the relevant connection request.
jar
If you have a UNIX background, you might have realized the definite similariries beteen JAR options and
the options of UNIX tar utility.
Behrouz Fallahi
Implicit Objects:
Implicit Objects in JSP are objects that are automatically available in JSP. Implicit Objects
are Java objects that the JSP Container provides to a developer to access them in their
program using JavaBeans and Servlets. These objects are called implicit objects because
they are automatically instantiated.
There are many implicit objects available. Some of them are:
request:
The
class
or
the
interface
name
of
the
object
request
is
http.httpservletrequest.
The
object
request
is
of
type
Javax.servlet.http.httpservletrequest. This denotes the data included with the HTTP
Request. The client first makes a request that is then passed to the server. The requested
object is used to take the value from clients web browser and pass it to the server. This is
performed using HTTP request like headers, cookies and arguments.
response: This denotes the HTTP Response data. The result or the information from a
request is denoted by this object. This is in contrast to the request object. The class or the
interface name of the object response is http.HttpServletResponse. The object response
is of type Javax.servlet.http. >httpservletresponse. Generally, the object response is
used with cookies. The response object is also used with HTTP Headers.
Session: This denotes the data associated with a specific session of user. The class or the
interface name of the object Session is http.HttpSession. The object Session is of type
Javax.servlet.http.httpsession. The previous two objects, request and response, are
used to pass information from web browser to server and from server to web browser
respectively. The Session Object provides the connection or association between the client
and the server. The main use of Session Objects is for maintaining states when there are
multiple page requests. This will be explained in further detail in following sections.
Out: This denotes the Output stream in the context of page. The class or the interface
name of the Out object is jsp.JspWriter. The Out object is written:
Javax.servlet.jsp.JspWriter
PageContext: This is used to access page attributes and also to access all the namespaces
associated with a JSP page. The class or the interface name of the object PageContext is
jsp.pageContext. The object PageContext is written: Javax.servlet.jsp.pagecontext
Application: This is used to share the data with all application pages. The class or the
interface name of the Application object is ServletContext. The Application object is
written: Javax.servlet.http.ServletContext
Config: This is used to get information regarding the Servlet configuration, stored in the
Config object. The class or the interface name of the Config object is ServletConfig. The
object Config is written Javax.servlet.http.ServletConfig
Page: The Page object denotes the JSP page, used for calling any instance of a Page's
servlet. The class or the interface name of the Page object is jsp.HttpJspPage. The Page
object is written: Java.lang.Object
The most commonly used implicit objects are request, response and session objects
10
getAttribute(String name)
getAttributeNames
isNew()
getCreationTime
getId
invalidate()
getLastAccessedTime
getMaxInactiveInterval
removeAttribute(String name)
setAttribute(String, object)
Detailed usage with syntax and example with explanation of each of these methods.
getAttribute(String name):
The getAttribute method of session object is used to return the object with the specified
name given in parameter. If there is no object then a null value is returned.
General syntax of getAttribute of session object is as follows:
session.getAttribute(String name)
The value returned is an object of the corresponding name given as string in parameter. The
returned value from the getAttribute() method is an object written: java.lang.Object.
For example:
getAttributeNames:
The getAttributeNames method of session object is used to retrieve all attribute names
associated with the current session. The name of each object of the current session is
returned. The value returned by this method is an enumeration of objects that contains all
the unique names stored in the session object.
General Syntax:
session.getAttributeNames()
11
exforsys = session.getAttributeNames( )
The above statement returns enumeration of objects, which contains all the unique names
stored in the current session object in the enumeration object exforsys.
isNew():
The isNew() method of session object returns a true value if the session is new. If the
session is not new, then a false value is returned. The session is marked as new if the
server has created the session, but the client has not yet acknowledged the session. If a
client has not yet chosen the session, i.e., the client switched off the cookie by choice, then
the session is considered new. Then the isNew() method returns true value until the client
joins the session. Thus, the isNew() method session object returns a Boolean value of true
of
false.
General syntax of isNew() of session object is as follows:
session.isNew()
The returned value from the above method isNew() is Boolean
12