Servlet Programming Tasks 9-19
Package weblogic.servlet.utils; public interface URLMapping {
public void putString pattern, Object value; public Object getString uri;
public void removeString pattern public void setDefaultObject defaultObject;
public Object getDefault; public void setCaseInsensitiveboolean ci;
public boolean isCaseInsensitive; public int size;
public Object[] values; public String[] keys;
}
9.15 The SimpleApacheURLMatchMap Utility
The included SimpleApacheURLMatchMap utility is not J2EE specific. It can be configured in the weblogic.xml deployment descriptor file and allows the user to
specify Apache style pattern matching rather than the default URL pattern matching provided in the web.xml deployment descriptor. For more information, see
Section B.16, url-match-map .
9.16 A Future Response Model for HTTP Servlets
In general, WebLogic Server processes incoming HTTP requests and the response is returned immediately to the client. Such connections are handled synchronously by
the same thread. However, some HTTP requests may require longer processing time. Database connection, for example, may create longer response times. Handling these
requests synchronously causes the thread to be held, waiting until the request is processed and the response sent.
To avoid this hung-thread scenario, WebLogic Server provides two classes that handle HTTP requests asynchronously by de-coupling the response from the thread that
handles the incoming request. The following sections describe these classes.
9.16.1 Abstract Asynchronous Servlet
The Abstract Asynchronous Servlet enables you to handle incoming requests and servlet responses with different threads. This class explicitly provides a better general
framework for handling the response than the Future Response Servlet, including thread handling.
You implement the Abstract Asynchronous Servlet by extending the weblogic.servlet.http.AbstractAsyncServlet.java class. This class
provides the following abstract methods that you must override in your extended class.
9.16.1.1 doRequest
This method processes the servlet request. The following code example demonstrates how to override this method.
Example 9–3 Overriding doRequest in AbstractAsynchServlet.java
public boolean doRequestRequestResponseKey rrk throws ServletException, IOException {
HttpServletRequest req = rrk.getRequest;
9-20 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server
HttpServletResponse res = rrk.getResponse; if req.getParameterimmediate = null {
res.setContentTypetexthtml; PrintWriter out = res.getWriter;
out.printlnHello World Immediately; return false ;
} else {
TimerManagerFactory.getTimerManagerFactory .getDefaultTimerManager.schedule
new TimerListener { public void timerExpiredTimer timer
{try { AbstractAsyncServlet.notifyrrk, null;
} catch Exception e {
e.printStackTrace; }
} }, 2000;
return true; }
}
9.16.1.2 doResponse
This method processes the servlet response.
If an exception occurs during processing, the container returns an error to the client. The following code example demonstrates how to override this method.
Example 9–4 Overriding doResponse in AbstractAsyncServlet.java
public void doResponse RequestResponseKey rrk, Object context throws ServletException, IOException
{ HttpServletRequest req = rrk.getRequest;
HttpServletResponse res = rrk.getResponse;
res.setContentTypetexthtml; PrintWriter out = res.getWriter;
out.printlnHello World; }
9.16.1.3 doTimeOut
This method sends a servlet response error when the notify method is not called within the timeout period.
Note: The servlet instance that processed the doRequest method
used to handle the original incoming request method will not necessarily be the one to process the doResponse method.
Servlet Programming Tasks 9-21
Example 9–5 Overriding doTimeOut in AbstractAsyncServlet.java
public void doTimeout RequestResponseKey rrk throws ServletException, IOException
{ HttpServletRequest req = rrk.getRequest;
HttpServletResponse res = rrk.getResponse;
res.setContentTypetexthtml; PrintWriter out = res.getWriter;
out.printlnTimeout; }
9.16.2 Future Response Servlet