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.

Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
xuanhung2401
  • 311
  • 1
  • 5
  • 15
  • 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 – Matthias Bruns Feb 09 '12 at 10:32
  • yes, category and sub category are dynamic. Can you show me a little example with request params ? – xuanhung2401 Feb 09 '12 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 – Matthias Bruns Feb 09 '12 at 10:47
  • UrlRewriteFilter solved my problem.Btw, thank you very much. – xuanhung2401 Feb 09 '12 at 10:58

5 Answers5

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

Kris
  • 5,714
  • 2
  • 27
  • 47
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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
0
kisp
  • 6,402
  • 3
  • 21
  • 19
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.

Paul Whelan
  • 16,574
  • 12
  • 50
  • 83
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
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.