aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet-mapping  StackOverFlow" Watch "servlet-mapping  StackOverFlow" New topic
Author

servlet-mapping StackOverFlow

Shin Hashitani
Ranch Hand

Joined: Dec 04, 2001
Posts: 54
Tomcat4.0.0
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.
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
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);
request.getRequestDispatcher("/jsp/form.jsp").forward(request,response);
} 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(ApplicationHttpRequest.java:233)
//lots of the same line continues...
at org.apache.catalina.core.ApplicationHttpRequest.removeAttribute(ApplicationHttpRequest.java:233)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:676)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:431)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:355)
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 /> * hashitani@quants.co.jp<br /> * <a href="http://www.quants.co.jp" target="_blank" rel="nofollow">www.quants.co.jp</a><br />*/
Jignesh Malavia
Author
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
-j
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: http://aspose.com/file-tools
 
subject: servlet-mapping StackOverFlow
 
Similar Threads
form submit
CMS - Approach to handle requests using directory names
How do I run a JSP?
Serlet request Dispatcher
RequestDispatcher.forward not working