I have a servlet. But instead of making that servlet listen to a static URL (e.g: /testservlet), I want it to listen to a dynamic url, or a URL with regex (e.g: /testservlet[0-9]*) which in this case will listen to a url mysite.com/testservlet followed by any numbers.
How do I do that?
-
what do you mean by listen to URL and why you need multiple urls for the same thing ?SMA– SMA2014-12-20 06:32:53 +00:00Commented Dec 20, 2014 at 6:32
-
@almasshaikh like each servlet/JSP is listening to a specific url params (eg "/test"), so every time mysite.com/test is called, the servlet's method service() is calledVictor2748– Victor27482014-12-20 06:34:08 +00:00Commented Dec 20, 2014 at 6:34
-
test is specific then why regex?SMA– SMA2014-12-20 06:38:15 +00:00Commented Dec 20, 2014 at 6:38
-
@almasshaikh instead of listening to /homepage, I want it to listen to a url with dynamic pars (like home[ANY_NUMBERS_HERE]page). Doesn't have to be regexVictor2748– Victor27482014-12-20 06:40:13 +00:00Commented Dec 20, 2014 at 6:40
Add a comment
|
1 Answer
Assuming that your servlet class is: victor.serlets.MyServlet
In your application's web.xml, include the following:
<servlet>
<servlet-name>myTestServlet </servlet-name>
<servlet-class>victor.serlets.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myTestServlet</servlet-name>
<url-pattern>/testservlet*</url-pattern>
</servlet-mapping>
As for the wildcard in the url-pattern tag, if you want to be more specific than "*" (i.e. anything), I am not certain what wildcards are supported. I suggest you look at the Oracle servlet docs for more info. FWIW, I think the "*" wildcard will suffice for your app.