Professional Documents
Culture Documents
Servlet Manipulation
Direct servlet manipulation involves one servlet accessing the loaded
servlets on its server and optionally performing some task on one or
more of them. A servlet obtains information about other servlets
through the ServletContext object. Use getServlet() to get a particular
servlet:
public Servlet ServletContext.getServlet(String name) throws
ServletException
This method returns the servlet of the given name, or null if the
servlet is not found.
Viewing the Currently Loaded Servlets
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
Servlet Reuse
The major challenge with servlet reuse is for the "user" servlet to
obtain the proper instance of "usee" servlet when the usee servlet
has not yet been loaded into the server.
Directly creating a new instance of the usee servlet doesn't work
either, as the newly created servlet doesn't have access to its own
ServletConfig and ServletContext objects. Servlet API distinctly
lacks any methods whereby a servlet can control the servlet life cycle,
for itself or for other servlets. This is considered a security risk and is
officially "left for future consideration."
Servlet Collaboration
Sometimes servlets have to cooperate, usually by sharing some
information. We call communication of this sort servlet collaboration.
Collaborating servlets can pass the shared information directly from
one servlet to another through method invocations, as shown earlier.
This approach requires each servlet to know the other servlets with
which it is collaborating--an unnecessary burden. There are several
better techniques.
Filters in servlet
<filter>
<filter-name>FilterMyDocs</filter-name>
<filter-class>mypackage1.FilterTwo</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterMyDocs</filter-name>
<url-pattern>/mydocs/*</url-pattern>
</filter-mapping>
SingleThreadModel in Servlet
Servlet in java application can be accessed by multiple users at a
same time
SingleThreadModel in servlet, we can use it to restrict access of more
than one user at a same time. SingleThreadModel is useful in case
you required to process one request at one time, no other can access
that process when servlet is in use.
To make servlet a Single thread, need to implement
SingleThreadModel interface. This is an empty, tag interface that
defines no methods or variables and serves only to flag the servlet as
wanting the alternate life cycle.
The example is given of SingleThreadModel in Servlet.
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
Servlet Chaining
// FirstServlet
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
response.setContentType("text/plain");
if(name!=null) {
request.setAttribute("UserName",name);
rd.forward(request , response);
// Forward the value to another Secondservlet
} else {
response.sendError(response.SC_BAD_REQUEST,
"UserName Required");
}
}
}
// SecondServlet
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class SecondServlet extends HttpServlet {
public void doGet(HttpServletRequest request ,
HttpServletResponse response)
throws ServletException , IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
String UserName = (String)request.getAttribute("UserName");
Context Attributes.
A servlet can bind an object attribute into the context by name. Any
attribute bound into a context is available to any other servlet that is
part of the same Web application. The following methods
of ServletContext interface allow access to this functionality:
setAttribute
Binds an object to a given attribute name in this servlet context. If the
name specified is already used for an attribute, this method will
REPLACE the attribute with the new to the new attribute. If listeners
are configured on the ServletContext the container notifies them
accordingly. If a null value is passed, the effect is the same as
calling removeAttribute(). Attribute names should follow the same
convention as package names. The Java Servlet API specification
reserves names matching java.*, javax.*, and sun.*.
getAttribute
Returns the servlet container attribute with the given name, or null if
there is no attribute by that name. An attribute allows a servlet
container to give the servlet additional information not already
provided by this interface. See your server documentation for
information about its attributes. A list of supported attributes can be
retrieved using getAttributeNames. The attribute is returned as
ajava.lang.Object or some subclass. Attribute names should follow
the same convention as package names. The Java Servlet API
specification reserves names matching java.*, javax.*, and sun.*.
getAttributeNames
Returns an Enumeration containing the attribute names available
within this servlet context. Use the getAttribute(String) method with an
attribute name to get the value of an attribute.
removeAttribute
Removes the attribute with the given name from the servlet context.
After removal, subsequent calls to getAttribute(String) to retrieve the
attribute’s value will return null. If listeners are configured on
the ServletContext the container notifies them accordingly.