|
11) What are all the different kinds of
servers? (Such as Web Servers, Application Servers,
etc)
The servers involved in handling and
processing a user's request break down into a few basic types, each of which
may have one or more tasks it solves. This flexibility gives developers a
great deal of power over how applications will be created and deployed, but
also leads to confusion over what server is able to, or should, perform a
specific task.
Starting at the basic level, a user is
typically submitting a request to a system through a web browser. (We are
conveniently ignoring all other types of clients (RMI, CORBA, COM/DCOM,
Custom, etc..) for the time being for purposes of clarity.) The web request
must be received by a Web Server (otherwise known as an HTTP
Server) of some sort. This web server must handle standard HTTP requests
and responses, typically returning HTML to the calling user. Code that
executes within the server environment may be CGI driven, Servlets, ASP, or
some other server-side programming language, but the end result is that the
web server will pass back HTML to the user.
The web server may need to execute an
application in response to the users request. It may be generating a list of
news items, or handling a form submission to a guest book. If the server
application is written as a Java Servlet, it will need a place to execute, and
this place is typically called a Servlet Engine. Depending on the web
server, this engine may be internal, external, or a completely different
product. This engine is continually running, unlike a traditional CGI
environment where a CGI script is started upon each request to the server.
This persistance gives a servlet connection and thread pooling, as well as an
easy way to maintain state between each HTTP request. JSP pages are usually
tied in with the servlet engine, and would execute within the same
space/application as the servlets.
There are many products that handle the web
serving and the servlet engine in different manners. Netscape/iPlanet
Enterprise Server builds the servlet engine directly into the web server and
runs within the same process space. Apache requires that a servlet engine run
in an external process, and will communicate to the engine via TCP/IP sockets.
Other servers, such as MS IIS don't officially support servlets, and require
add-on products to add that capability.
When you move on to Enterprise JavaBeans (and
other J2EE components like JMS and CORBA) you move into the application server
space. An Application Server is any server that supplies additional
functionality related to enterprise computing -- for instance, load balancing,
database access classes, transaction processing, messaging, and so on.
EJB Application
Servers provide an EJB container,
which is the environment that beans will execute in, and this container will
manage transactions, thread pools, and other issues as necessary. These
application servers are usually stand-alone products, and developers would tie
their servlets/JSP pages to the EJB components via remote object access APIs.
Depending on the application server, programmers may use CORBA or RMI to talk
to their beans, but the baseline standard is to use JNDI to locate and create
EJB references as necessary.
Now, one thing that confuses the issue is that
many application server providers include some or all of these components in
their product. If you look at WebLogic (http://www.beasys.com/) you will find
that WebLogic contains a web server, servlet engine, JSP processor, JMS
facility, as well as an EJB container. Theoretically a product like this could
be used to handle all aspects of site development. In practice, you would most
likely use this type of product to manage/serve EJB instances, while dedicated
web servers handle the specific HTTP requests.
12) Should I override the service()
method?
No. It provides a fair bit of housekeeping
that you'd just have to do yourself. If you need to do something regardless of
whether the request is e.g., a POST or a GET, create a helper method and call
that at the beginning of e.g., doPost() and doGet().
13) How can my application get to know when
a HttpSession is removed (when it
time-outs)?
Define a class, say SessionTimeoutNotifier,
that implements javax.servlet.http.HttpSessionBindingListener. Create a
SessionTimeoutNotifier object and add it to the user
session. When the session is removed, SessionTimeoutNotifier.valueUnbound()
will be called by the servlet engine. You can implement valueUnbound()
to do whatever you want.
14) Why use JSP when we can do the same
thing with servlets?
[Original question: Why should I use JSP when
there is already servlet technology available for serving dynamic content?]
While JSP may be great for serving up dynamic
Web content and separating content from presentation, some may still wonder
why servlets should be cast aside for JSP. The utility of servlets is not in
question. They are excellent for server-side processing, and, with their
significant installed base, are here to stay. In fact, architecturally
speaking, you can view JSP as a high-level abstraction of servlets that is
implemented as an extension of the Servlet 2.1 API. Still, you shouldn't use
servlets indiscriminately; they may not be appropriate for everyone. For
instance, while page designers can easily write a JSP page using conventional
HTML or XML tools, servlets are more suited for back-end developers because
they are often written using an IDE -- a process that generally requires a
higher level of programming expertise.
When deploying servlets, even developers have
to be careful and ensure that there is no tight coupling between presentation
and content. You can usually do this by adding a third-party HTML wrapper
package like htmlKona to the mix. But even this approach, though providing
some flexibility with simple screen changes, still does not shield you from a
change in the presentation format itself. For example, if your presentation
changed from HTML to DHTML, you would still need to ensure that wrapper
packages were compliant with the new format. In a worst-case scenario, if a
wrapper package is not available, you may end up hardcoding the presentation
within the dynamic content. So, what is the solution? One approach would be to
use both JSP and servlet technologies for building application systems.
15) How do I send information and data back
and forth between applet and servlet using the HTTP
protocol?
Use the standard java.net.URL
class, or "roll your own" using java.net.Socket.
See the HTTP spec at
W3C for more detail.
Note: The servlet cannot initiate this
connection! If the servlet needs to asynchronously send a message to the
applet, then you must open up a persistent socket using java.net.Socket
(on the applet side), and java.net.ServerSocket and
Threads (on the server side).
16) Can I get the path of the current
servlet where it lives on the file system (not its
URL)?
Try using:
request.getRealPath(request.getServletPath())
An example may be:
out.println(request.getRealPath(request.getServletPath()));
17) How can I daisy chain servlets together
such that the output of one servlet serves as the input to the
next?
There are two common methods for chaining the
output of one servlet to another servlet :
ï‚· the first method
is the aliasing which describes a series of servlets to be executed
ï‚· the second one
is to define a new MIME-Type and associate a servlet as handlers As I don't
really use the second one, I'll focus on the aliasing.
To chain servlets together, you have to
specify a sequential list of servlets and associate it to an alias. When a
request is made to this alias, the first servlet in the list is invoked,
processed its task and sent the ouptut to the next servlet in the list as the
request object. The output can be sent again to another servlets.
To accomplish this method, you need to
configure your servlet engine (JRun, JavaWeb server, JServ ...).
For example to configure JRun for servlet chaining, you select the JSE service
(JRun servlet engine) to access to the JSE Service Config panel. You have just
to define a new mapping rule where you define your chaining servlet.
Let say /servlets/chainServlet for the virtual path and a comma separated list
of servlets as srvA,srvB.
So when you invoke a request like http://localhost/servlets/chainServlet,
internally the servlet srvA will be invoked first and its results will be
piped into the servlet srvB.
The srvA servlet code should look like :
public class srvA extends HttpServlet {
...
public void doGet (...) {
PrintWriter out =res.getWriter();
rest.setContentType("text/html");
...
out.println("Hello Chaining servlet");
}
}
All the servlet srvB has to do is to open an
input stream to the request object and read the data into a BufferedReader
object as for example :
BufferedReader b = new BufferedReader( new InputStreamReader(req.getInputStream() ) );
String data = b.readLine();
b.close();
After that you can format your output with the
data.
It should work straigthforward with Java Web
Server or Jserv too. Just look at in their documentation to define an alias
name. Hope that it'll help.
18) Why there are no constructors in
servlets?
A servlet is just like an applet in the
respect that it has an init() method that acts as a constrcutor. Since the
servlet environment takes care of instantiating the servlet, an explicit
constructor is not needed. Any initialization code you need to run should be
placed in the init() method since it gets called when the servlet is first
loaded by the servlet container.
19) How to handle multiple concurrent
database requests/updates when using JDBC with
servlets?
All the dbms provide the facility of locks
whenever the data is being modified. There can be two scenarios:
-
Multiple database updates on different rows,
if you are using servlets the servlets will open multiple connections for
different users. In this case there is no need to do additional programming.
-
If database updates are on the same row then
the rows are locked automatically by the dbms, hence we have to send
requests to the dbms repeatatively until the lock is released by dbms.
This issue is dealt with in the JDBC
documentation; look up "Transactions" and "auto-commit". It can get pretty
confusing.
20) What is the difference between
GenericServlet and
HttpServlet?
GenericServlet is for servlets that might not
use HTTP, like for instance FTP servlets. Of course, it turns out that there's
no such thing as FTP servlets, but they were trying to plan for future growth
when they designed the spec. Maybe some day there will be another subclass,
but for now, always use HttpServlet.
java6 ejb3 jsf hibernate eclipse ajax groovy spring seam java struts webservice j2me guice java5 jca tapestry soa linux ria books
|