0

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?

4
  • what do you mean by listen to URL and why you need multiple urls for the same thing ? Commented 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 called Commented Dec 20, 2014 at 6:34
  • test is specific then why regex? Commented 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 regex Commented Dec 20, 2014 at 6:40

1 Answer 1

1

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.

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

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.