views:

1062

answers:

2

Suppose we have some project with next structure:

web
  articles
    main.jsp
    sidearts.jsp
    central.jsp
  forum
    main.jsp
  css
  js
  WEB-INF
    web.xml

Note that we don't have front controller at this point yet.

After deploying with some facet (let it be 'asdf') we can access our pages using next URLs:

http://localhost:8080/asdf/articles/main.jsp
http://localhost:8080/asdf/forum/main.jsp

and so on..

main.jsp generates some html and includes sidearts.jsp (by means of jstl c:import or any other way)

And what will happen after adding front controller?

Suppose we have servlet ArticlesController which is responsible for dispatching
some requests and which has next mapping:

<servlet>
  <servlet-name>ArtsController</servlet-name>
  <servlet-class>org.forstackoverflow.ArticlesController</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>ArtsController</servlet-name>
  <url-pattern>/articles/*</url-pattern>
</servlet-mapping>

Now when we request URL http://localhost:8080/asdf/articles/main, ArticlesController handles this request and try to include articles/main.jsp. And at this point infinity cycle starts because /articles/* is mapped to ArtsController.

What is the correct solution of described problem?

My variants are:

1) make mappings for all jsp-files (I don't think that it is acceptible)

2) change directories names (articles->arts); but then we get lots of new URLs (like http://localhost:8080/asdf/arts/main.jsp) and I think that it can be a source of bugs.

+1  A: 

You may be confusing servlets with filters. With servlet, there will be no infinite loop. Your JSP pages will always be mapped via exact patterns, either explicitly (if they were precompiled) or implicitly (via JSP file path relative to webapp root if they weren't precompiled). What this means is your "articles/main.jsp" will effectively be a servlet with the following mapping:

<servlet-mapping>
  <servlet-name>name_does_not_matter_here</servlet-name>
  <url-pattern>/articles/main.jsp</url-pattern>
</servlet-mapping>

With your ArticlesController servlet mapped to /articles/* the following will happen:

http://localhost:8080/asdf/articles/main URL will be handled by your servlet because it does not match the pattern for the JSP. However, http://localhost:8080/asdf/articles/main.jsp URL which matches both patters will be mapped to the JSP page and not the servlet because servlet container will ALWAYS prefer exact match over wildcard match (this is a part of J2EE spec).

ChssPly76
Is there a way to deploy the same servlet multiple times without needing extra copies of it? Say I write some CMS and I want two customers to be able to use it - is this a valid use-case? Is this controlled via web.xml?
Chris Kaminski
I'm not quite sure what you mean or how it relates to the original question... If by "deploy multiple times" you mean "map to different urls" then yes, you can do that.
ChssPly76
A: 

Since you are using MVC, you should let the client (i.e. a browser) access to the view, the jsp in this case. The controller servlet should be called from the view when an action is performed on it. After processing the action in the controller, you redirect to the next view (which could be the same that started the action.)

Making the servlet url-pattern match to a real directory mess things up, because it tells the container to choose the servlet instead of the default page for that directory.

df