Spring MVC - POST Request With Simple Form Submission MCQ
0/13
0✓
0✗
1. Which HTML form attribute tells the browser to send the form data using an HTTP POST request?
Why: The method attribute of the <form> tag sets the HTTP method. Use method="post" to submit the form with POST.
2. What does the action attribute of an HTML form specify?
Why: The action attribute holds the URL that receives the submitted form. In Spring MVC, a controller method mapped to that URL handles the request.
3. What is the default HTTP method used by an HTML form if the method attribute is not given?
Why: If you do not set the method attribute, the browser sends the form using GET. You must set method="post" to use POST.
4. How does a simple HTML form send its field values in a POST request?
Why: With POST, the browser puts the field values in the request body as name=value pairs joined by &. With GET, they would go in the URL query string instead.
5. Why is POST usually a better choice than GET for submitting form data?
Why: POST sends data in the request body, so it does not appear in the browser address bar. It is also the normal choice for requests that change data on the server. POST alone does not encrypt anything.
6. Which annotation is a shortcut for mapping a controller method to HTTP POST requests?
Why:@PostMapping is a shortcut for @RequestMapping(method = RequestMethod.POST). It maps POST requests to the annotated method.
7. Which annotation is used to bind a submitted form field to a controller method parameter?
Why:@RequestParam binds a request parameter, such as a submitted form field, to a method parameter. @PathVariable is for values inside the URL path.
8. How does Spring match a form field to a method parameter annotated with @RequestParam?
Why: The browser sends each field using its name attribute. Spring uses that name to find the matching @RequestParam value.
9. By default, what happens if a required @RequestParam value is missing from the request?
Why: A @RequestParam is required by default. If the parameter is missing, Spring responds with 400 Bad Request. Use required = false or a defaultValue to make it optional.
10. A controller class is annotated as shown below. For which URL and HTTP method does the create() method handle requests?
@Controller
@RequestMapping("/orders")
public class OrderController {
@PostMapping
@ResponseBody
public String create() {
return "ok";
}
}
Why: The class-level @RequestMapping sets the base path. A @PostMapping without a path uses that base path, so the method handles POST requests to /orders.
11. What is the purpose of the @ResponseBody annotation on a controller method?
Why: With @ResponseBody, the returned value is written directly to the response. Without it, a returned String is treated as a view name.
12. What is the role of the DispatcherServlet when a form is submitted to a Spring MVC application?
Why:DispatcherServlet is the front controller of Spring MVC. It receives incoming requests and passes each one to the controller method that matches the URL and HTTP method.
13. Which method of WebMvcConfigurer is used to serve static files, such as HTML pages, from a folder in the web application?
Why:addResourceHandlers() maps a URL pattern, such as /static/**, to a location that holds static files like HTML, CSS and images.
14. Which annotation is used on a Java configuration class to enable Spring MVC features?
Why:@EnableWebMvc turns on the Spring MVC configuration, including support for annotations like @PostMapping and @RequestParam.
15. When testing from the command line, what does the -d option of curl do?
Why: With curl -d "name=value", the data is sent in the request body as standard form data. Using -d also makes curl use the POST method by default.