Servlet concepts - Technicalsymposium

mobdescriptiveSoftware and s/w Development

Oct 28, 2013 (4 years and 5 months ago)



Are sessions created on the server side?


A servlet session is created and stored on the server side. The servlet container
keeps track of all the sessions it manages and fulfils servlet API requests to get
, manipulate object data stored wi
th them and trigger event

To the maintain the session, Web clients must pass back a valid session identifier
as a cookie header value or dynamically generated URL parameter. In this sense,
the session is also stored by the client, but only as a

token reference.


Can I create a session with


There are no protocol
specific features in
, which is an
implementation of the basic, general purpose

interface. Servlet
sessions are designed only for intera
ctions using the HTTP protocol, which has
two key features necessary for a servlet container to simulate continuous user
sessions: cookies and URL
based navigation, which supports URL
rewriting. The
servlet API therefore places the

interface in


package, and session references are only available through
classes in this package.


How can I assemble data from multiple input forms?


First, it is best to use a single servlet to handle each form submission. A single
t for all input would be too complicated. Give each servlet responsibility to
validate a single form input, and pass the error cases on to JSP documents that
explain the problem and allow users to amend the input.

Servlet concepts


What's the differen
ce between applets and servlets?


There are many fundamental differences between


classes, the Java API documentation for the two types will show you they have
little in common.

Applets are essentially graphical user interface (GUI) a
pplications that run on the
client side in a network environment, typically embedded in an HTML page.
Applets are normally based on Abstract Windowing Toolkit components to
maintain backward
compatibility with the widest range of browsers' Java
ions. The application classes are downloaded to the client and run in
a Java Virtual Machine provided by the browser, in a restrictive security
environment called a "sandbox".

Servlets are used to dynamically generate HTTP responses and return HTML
t to Web browsers on the server side. Servlets are often used to validate and


process HTML form submissions and control a series of user interactions in what
is known as a Web application. Servlets can be used to control all aspects of the
request and resp
onse exchange between a Web browser and the server, called a
servlet container.


How can I write a servlet using Javascri pt?


Java servlets is a server side technology that delivers dynamic content to Web
browsers and other clients. Javascript is als
o delivered by a Web server, but the
code is only interpreted and executed after it has been downloaded by the Web
browser. This means that it is not possible to write servlet code in Javascript.

It is possible to

Javascript in the output of servl
ets and Java Server Pages,
just like standard Web pages. It is also possible to dynamically generate
Javascript using a servlet and use it as the source for a script tag, though this is
only advisable in rare cases.


How does the JVM execute a servlet c
ompared with a regular Java class?


Servlets are standard Java classes and are executed by the Java Virtual
Machine in exactly the same way as any other. However, the environment or
context in which servlets are executed is different. A servlet is not i
directly through a

method, the class is loaded and run by a servlet container.

Can I include normal Java classes in servlets?


Any Java class can be used in a Web application, provided you make the
classes available to the servlet contai
ner at runtime. The Java API classes can be
used directly by adding import statements to your servlet class. Other supporting
classes can also be imported, but these classes must be added to the


directory of your application.

If you need to

configure the supporting classes, this can be done with standard
servlet configuration features using the


objects available to the



How can I tell when a servlet is instantiated?


A servle
t must be instantiated before it is brought into service by the servlet
container, so one way to check is to make a request to the servlet and check the
response. If you need to check indirectly, you can override the

method and add

statements to it. This
method is called after the servlet container has instantiated the servlet before it is
brought into service.


What is URL
rewri ti ng?



rewriting is a way of maintaining a session between an HTTP client and
a servlet

container which does not use cookies. Rather than exchange a session ID
in a cookie, the servlet container includes it in the hyperlink URLs it generates for
servlets and JSP.


Are servlets multi


Yes, servlets are normally multi
. The servlet container allocates a
thread for each new request for a single servlet without any special programming.
Each thread of your servlet runs as if a single user were accessing it alone, but
you can use static variables to store and present inform
ation that is common to all
threads, like a hit counter for instance.

How can I pass values between JSPs without using sessions?


There are four main alternatives to full session
based transfer of data values,
two of them use features of the session tr
acking API: cookies and URL
detailed below.

What is JavaServer Pages technology?

JavaServer Pages (JSP) technology provides a simplified, fast way to create web pages
that display dynamically
generated content. The JSP specification, developed
through an
wide initiative led by Sun Microsystems, defines the interaction between the
server and the JSP page, and describes the format and syntax of the page.

How does the JavaServer Pages technology work?

JSP pages use XML tags and scriptlets

written in the Java programming language to
encapsulate the logic that generates the content for the page. It passes any formatting
(HTML or XML) tags directly back to the response page. In this way, JSP pages separate
the page logic from its design and d

JSP technology is part of the Java technology family. JSP pages are compiled into
servlets and may call JavaBeans components (beans) or Enterprise JavaBeans
components (enterprise beans) to perform processing on the server. As such, JSP
y is a key component in a highly scalable architecture for web

JSP pages are not restricted to any specific platform or web server. The JSP specification
represents a broad spectrum of industry input.

What is a servlet?

A servlet is
a program written in the Java programming language that runs on the server,
as opposed to the browser (applets). Detailed information can be found at

Why do I need
JSP technology if I already have servlets?


JSP pages are compiled into servlets, so theoretically you could write servlets to support
your web
based applications. However, JSP technology was designed to simplify the
process of creating pages by separating
web presentation from web content. In many
applications, the response sent to the client is a combination of template data and
generated data. In this situation, it is much easier to work with JSP pages
than to do everything with servlets.

re can I get the most current version of the JSP specification?

The JavaServer Pages 2.1 specification is available for download from

How does the JSP specification relate to
the Java Enterprise Edition 5 Platform?

The JSP 2.1 specification is an important part of the
Java EE 5 Platform
. Using JSP and
Enterprise JavaBeans technologies together is a great way to implement distributed
erprise applications with web
based front ends.

Which web servers support JSP technology?

There are a number of JSP technology implementations for different web servers. The
latest information on officially
announced support can be found at

Is Sun providing a reference implementation for the JSP specification?

The GlassFish project is Sun's free, open
source Java EE 5 implementation.

It includes

an implementation of JSP technology version 2.1.

You can download GlassFish builds

How is JSP technology different from other products?

JSP technology is the result o
f industry collaboration and is designed to be an open,
standard method supporting numerous servers, browsers and tools. JSP
technology speeds development with reusable components and tags, instead of relying
heavily on scripting within the page i
tself. All JSP implementations support a Java
programming language
based scripting language, which provides inherent scalability and
support for complex operations.

Where do I get more information on JSP technology?

The first place to check for informatio
n on JSP technology is
. This site includes numerous resources, as well as
pointers to mailing lists and discussion groups for JSP technology
related topics.