0

I have a servlet called Document to which I want to redirect all requests starting with Document something like

http://localhost:8080/CollabEdit/Document/abcdwcklsclds 

should be redirected to servlet Document. So, I used an annotation like this :

@WebServlet("/Document/*")

However, for some unidentified reason, it gives an exception that says:

javax.servlet.ServletException: AS-WEB-CORE-00089

This exception is in Document.java is thrown as soon as I call

request.getRequestDispatched("main.html").forward(request, response) . otherwise no exception.

however, With same request in other servlets, main.html gets called just fine.

Simran kaur
  • 27
  • 1
  • 7
  • As already said in your previous question, please show stack trace, relevant parts of the servlet : the class declaration including annotations and the method containing the `forward`, and the type and version of servlet container (Tomcat, Glassfish, ...) – Serge Ballesta Mar 25 '15 at 14:39
  • @SergeBallesta: Here is what I realized.The error is because I have request.getRequestDispatcher.forward("main.html") statement in doGet method of Document.java and problem is that for some reason statements in doGet gets called over and over when we have RequestDispatcher in there .Last time it was 17 times , because of which we are getting this error. Could you help me out with possible reasons,please ? – Simran kaur Mar 25 '15 at 15:27
  • Without the elements I asked I cannot say more : if it is still same problem, it is caused by the forward calling the current servlet what gives an infinite loop. – Serge Ballesta Mar 25 '15 at 15:31
  • and I forgot to mention that I am using Glassfish. But, I am not really calling the current servlet but an html file. – Simran kaur Mar 25 '15 at 15:33
  • I first thought of a typo, but it is the real reason. See my answer. – Serge Ballesta Mar 25 '15 at 16:11

2 Answers2

1

A forward and a redirect are not the same thing. A redirect allows you to go to any URL, eventually on another server or with another protocol, because you ask the client to query that URL.

In a forward you ask the servlet container to pass the control to another servlet of the same application (inside same context). As you use a relative path you are actually requesting what is the servlet for : http://host.do.main/appname/Document/main.html because the relative URL is added at the end of the address of current page (it could even be .../Document/.../main.html) !

And you declared that any page under /Document should be served by the Document servlet ... so the infinite loop ...

You can fix it two ways :

  • if you really need to use relative paths (it is inherently dangerous but you might have reasons to do it), try ../main.html if called from /Document or ../../main.html if called from /Document/something
  • use an absolute path :

    contextPath = request.getContextPath();
    request.getRequestDispatcher(contextPath + "/main.html").forward(request, response)
    
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

The Filter looks right.

Are you using Servlets 3.0 and no older version? In older versions you would have to edit the web.xml. And in 3.0 it would have to look like in this Post

Does Document extend HttpServlet?

Could you show us some more of your Code, maybe then it would be easier to see what's going wrong.

Greetings

Community
  • 1
  • 1
uBreckner
  • 110
  • 2
  • 8