3

I'm working on an HttpServlet and trying to define a url-pattern with a wildcard, but not finding much documentation.

The path I want to capture is "resource/{id}/action"

I've tried my annotation as:

@WebServlet("/resource/*/action")

but this doesn't match, though the more basic "resource/*" works okay.

Also, is there any way I can automatically pull out my {id} wildcard, rather than having to parse the url manually?

Nathan
  • 1,396
  • 3
  • 18
  • 32
  • possible duplicate of [can we user regular expression type patterns in web.xml?](http://stackoverflow.com/questions/8570805/can-we-user-regular-expression-type-patterns-in-web-xml) – KV Prajapati Nov 08 '12 at 04:19

1 Answers1

0

I'm think you try to solve wrong task. It's really unusual decision to map servlet on wildcard like this. Take a look on Spring MVC framework there you can write methods like this

@RequestMapping("/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)
public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
  Owner owner = ownerService.findOwner(ownderId);  
  Pet pet = owner.getPet(petId);  
  model.addAttribute("pet", pet);  
  return "displayPet"; 
}
Alexey Sviridov
  • 3,360
  • 28
  • 33
  • 1
    Is there a way to do this with plain Java and xml? – CodyBugstein Sep 11 '14 at 02:17
  • @Imray Of course, you can write simple hand-made dispatcher which will parse query string (i.e. with regex) in doGet/doPost methods. it's really simple – Alexey Sviridov Sep 12 '14 at 14:25
  • You mean if I want to capture `user/123/reports/r33` (in other words, the page representing report r33 filed by user 123), I should map `user/*` to some servlet `Parser` and run a whole regex parser in there? Sounds complicated – CodyBugstein Sep 14 '14 at 13:17
  • @Imray I tell more - better map dispatcher servlet to handle /* requests, and then parse and split it to parts by your rules. Yes, it's sounds complicated, but much easier and better then maintain bunch of servlet mappings (this all true only for raw servlets, of course) – Alexey Sviridov Sep 15 '14 at 05:21