Вопрос

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?

Это было полезно?

Решение

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"; 
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top