Servlet concepts - Technicalsymposium

mobdescriptiveΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 3 χρόνια και 7 μήνες)

68 εμφανίσεις


www.
Technicalsymposium
.com


Are sessions created on the server side?

A:

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
HttpSessions
, manipulate object data stored wi
th them and trigger event
callbacks.

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.

Q:

Can I create a session with
GenericServlet
?

A:

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

interface. Servlet
-
based
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
HttpSession

interface in

the
javax.servlet.http

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

Q:

How can I assemble data from multiple input forms?

A:

First, it is best to use a single servlet to handle each form submission. A single
servle
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

Q:

What's the differen
ce between applets and servlets?

A:

There are many fundamental differences between
Applet

and
Servlet

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
implementat
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
conten
t to Web browsers on the server side. Servlets are often used to validate and

www.
Technicalsymposium
.com


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.

Q:

How can I write a servlet using Javascri pt?

A:

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
include

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.

Q:

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

A:

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
nvoked
directly through a
main

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




Can I include normal Java classes in servlets?

A:

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
classes

or
lib

directory of your application.

If you need to

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

and
ServletContext

objects available to the
init(ServletConfig)

method.

Q:

How can I tell when a servlet is instantiated?

A:

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
init(ServletConfig)

method and add
log(
String)

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


Q:

What is URL
-
rewri ti ng?


www.
Technicalsymposium
.com


A:

URL
-
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
-
threaded?

A:

Yes, servlets are normally multi
-
threaded
. 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?

A:

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
-
rewriting,
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
industry
-
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
isplay.


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
technolog
y is a key component in a highly scalable architecture for web
-
based
applications.


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
http://java.sun.com/products/servlet
.

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


www.
Technicalsymposium
.com


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
dynamically
-
generated data. In this situation, it is much easier to work with JSP pages
than to do everything with servlets.

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

The JavaServer Pages 2.1 specification is available for download from
here
.

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
ent
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
http://java.sun.com/products/jsp/industry.html
.

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
from
https://glassfish.dev.java.net/
.



How is JSP technology different from other products?

JSP technology is the result o
f industry collaboration and is designed to be an open,
industry
-
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
http://java.sun.com/products/jsp/
. This site includes numerous resources, as well as
pointers to mailing lists and discussion groups for JSP technology
-
related topics.






www.
Technicalsymposium
.com