Junior — Middle
How can parameters be extracted from an incoming HTTP request?
sobes.tech AI
Answer from AI
In Java, parameters from an incoming HTTP request can be extracted in several ways depending on the framework used:
- In the Servlet API, through methods like
request.getParameter("paramName")to obtain parameters from the query string or form. - In Spring MVC, using annotations such as
@RequestParamfor request parameters,@PathVariablefor path variables, and@RequestBodyfor the request body.
Example with Spring MVC:
@GetMapping("/users")
public String getUser(@RequestParam String id) {
// id will be extracted from the request parameters
return "User id: " + id;
}