File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes servlet-mapping  StackOverFlow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet-mapping  StackOverFlow" Watch "servlet-mapping  StackOverFlow" New topic

servlet-mapping StackOverFlow

Shin Hashitani
Ranch Hand

Joined: Dec 04, 2001
Posts: 54
Windows 2000
NetBeans 3.3.2
IE 6.0
Hi everybody,
What I want to do is get all request to the application mapped to a single servlet.
From here, I want to dispatch to a proper servlet/JSP.

HttpSession session = request.getSession(true);
if (session.isNew()) {
FormDataBean dataBean = new FormDataBean();
session.setAttribute("FormData", dataBean);
} else {
FormDataBean dataBean = (FormDataBean)session.getAttribute("formData");
String destination = request.getRequestURI();
destination = destination.substring(destination.lastIndexOf("/") + 1, destination.length());
destination = destination.trim();

if(destination.equals("form.jsp") || destination.trim().equals("") || destination.trim().equals("javaFormMail")) {
request.getRequestDispatcher("/jsp/form.jsp").forward(request, response);
} else if(destination.equals("confirm.jsp")) {
request.getRequestDispatcher("/jsp/confirm.jsp").forward(request, response);
} else {
request.getRequestDispatcher("/jsp/error.jsp").forward(request, response);
Kind of like this. But I get StackOverFlow error:
at org.apache.catalina.core.ApplicationHttpRequest.removeAttribute(
//lots of the same line continues...
at org.apache.catalina.core.ApplicationHttpRequest.removeAttribute(
at org.apache.catalina.core.ApplicationDispatcher.invoke(
at org.apache.catalina.core.ApplicationDispatcher.doForward(
at org.apache.catalina.core.ApplicationDispatcher.forward(
Well, it's a stack over-flow.
I debugged the servlet from Netbeans, and as I found out, every single time the execution goes into first "if" clause, calling forward() method, and coming back to "doGet()" method of the same servlet. It's going around in a circle.
I made a small servlet that checks the value of session.isNew() and getRequestURI() value and display, and it looked good.
My conclusion is that forward() method is forwarding the request to itself. I don't mean to map forward to Controller servlet, too.
To be honest,
I read Servlet 2.3 specification, but it didn't make much of a difference.
How do I do to do what I am trying to do. I think its servlet-mapping, but I can't find out.
If you know a great resource for all servlet-mapping question, I will really appreciate it, too.
Thank you,
Quants Inc.
Tokyo, Japan
Shin Hashitani

/*<br /> * Quants Inc.<br /> * Engineer<br /> *<br /> * Shin Hashitani<br /> *<br /> * <a href="" target="_blank" rel="nofollow"></a><br />*/
Jignesh Malavia
Ranch Hand

Joined: May 18, 2001
Posts: 81
This is a very common problem when mapping /* to a servlet. A call to RequestDispatcher.include() or forward() within that servlet to any other resource (URL) matchs the /* url-pattern and the code enters into an infinite recursion.
Looking at your code, seems like it is doing the work of a Filter more than that of a Controller Servlet. Try using a Filter and map it to the /* url-pattern in deploymwnt descriptor. There are two advantages for doing that: First, it will not enter into an infinite recursion. The container does not invoke the same filter again if the request was forwarded from a filter. Second, if the session is not new, you will not have to do the string manipulation on the destination requestURI the way you are doing in the 'else' part of the code. Just let the request/response go through the chain via FilterChain.doFilter() and it will take care of itself.
hope that helps
Shin Hashitani
Ranch Hand

Joined: Dec 04, 2001
Posts: 54
Hi Jignesh,
Thank you very much for your posting.
I knew that there is a thing called Filter. I did a little work on the program, and now it works great. Since my boss rejected the idea of changing the specification dramatically at this stage of development, I decided to map all request with ".ctrl" to Controller as I did before, and made a filter to reject any request with non-ctrl extension. At least this works fine.
As a beginner,
it is always difficult to determine where the problem is. It was priceless to find out the solid fact on something what I wasn't sure. "/*" mapping is, I am sure, something I'll never do. It was a good lesten.
Thank you very much.

Shin Hashitani
I agree. Here's the link:
subject: servlet-mapping StackOverFlow
It's not a secret anymore!