1

i'm doing a News project.We have news category, news sub category, and news detail page. I want to have url like :

  • Category page : "http://mysite.com/my-dynamic-category".
  • Sub Category page : "http://mysite.com/my-dynamic-category/sub-category".
  • News detail page : "http://mysite.com/my-dynamic-category/sub-category/my-new-alias.html".

Three servlet : CategoryServlet, SubcategoryServlet, NewsDetailServlet. How can i map url with corresponding servlet in web.xml ? I am using eclipse and tomcat server.

4
  • So the categories will be dynamic and not static won't they? If so, you could use request params to handly, which category should be used Commented Feb 9, 2012 at 10:32
  • yes, category and sub category are dynamic. Can you show me a little example with request params ? Commented Feb 9, 2012 at 10:43
  • CONTEXT_ROOT/SOME_SERVLET/?maincategory=myMainCategory&subcategory=mysubcategory where maincategory and subcategory are the parameters and myMainCategory and mysubcategory are their values Commented Feb 9, 2012 at 10:47
  • UrlRewriteFilter solved my problem.Btw, thank you very much. Commented Feb 9, 2012 at 10:58

5 Answers 5

2

I guess that for this kind of task simple url mapping in web.xml is not enough. If you want to have dynamic urls mapped to your web resources (eg. servlets) you would have to do some url rewriting. The simplest would be to look for some URL Rewriting filter like the one from Tuckey with tutorial here: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html

Sign up to request clarification or add additional context in comments.

Comments

1

I map all urls into a single servlet in my webapp and let the web app itself decide how to serve them:

<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>com.myapp.Dispatcher</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

The disadvantage is that my servlet container no longer serves static files, I have to write code to load them and serve them through the web app or serve them on apache and configure it to not reverse proxy to tomcat for any static files.

2 Comments

i thought about it (like Strut framework), but looking for better way. May be urlrewritefilter is that way. Let me try. Thank you.
This person has an elegant way to deal wit the static content by extension: stackoverflow.com/a/3582215/1145388
0

If your sub-category is static, then you may use the url mapping as /*/sub-category where * maps to CategoryServlet.java, from there you may get the request path which contains /dynamic-category/sub-category, you may extract your sub-category and dynamic-category. With this you can also use only one servlet.

Comments

0

Eclipse has nothing with your problem, you could as well use vi or emacs. Your problem can be solved by URL rewriting ( either on reverse proxy side, or by something like: http://www.tuckey.org/urlrewrite/ ) - just remap your good locking URLs to real servlets.

Or you could just use a filter, parse the servlet path and use information to render your templates.

Comments

0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.