title: overriding doPost method in servlet()
body: <p>i am relatively new to spring mvc and have been exploring some form submit. However, currently, i have an error of HTTP 405 which would mean that i am unable to post. </p>
  
  <p>The error is that the HTTP post is not supported. I have googled and checked that i would need to override and implement doPost method in my code but i am unsure of how to use the servlet. </p>
  
  <p>By overriding the doPost method, how do I ensure that the new doPost method is applied?</p>
  
  <p>This is my controller class:</p>
  
  <pre><code>package com.**.web.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.authentication.AnonymousAuthenticationToken;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.servlet.ModelAndView;
  
  import com.**.dao.*;
  import com.**.model.User;
  
  @Controller
  public class MainController {
  
      @Autowired
      private UserDAO UserDAO;
  
      @RequestMapping(value = {"/hello", "/welcome**" }, method = RequestMethod.GET)
      public ModelAndView defaultPage() {
  
          ModelAndView model = new ModelAndView();
          model.addObject("title", "Spring Security Login Form - Database Authentication");
          model.addObject("message", "This is default page!");
          model.setViewName("hello");
          return model;
  
      }
  
      @RequestMapping(value = "/admin**", method = RequestMethod.GET)
      public ModelAndView adminPage() {
  
          ModelAndView model = new ModelAndView();
          model.addObject("title", "Spring Security Login Form - Database Authentication");
          model.addObject("message", "This page is for ROLE_ADMIN only!");
          model.setViewName("admin");
  
          return model;
  
      }
  
      @RequestMapping(value = {"/h", "/login"}, method = RequestMethod.GET)
      public ModelAndView login(@RequestParam(value = "error", required = false) String error,
              @RequestParam(value = "logout", required = false) String logout) {
  
          ModelAndView model = new ModelAndView();
          if (error != null) {
              model.addObject("error", "Invalid username and password!");
          }
  
          if (logout != null) {
              model.addObject("msg", "You've been logged out successfully.");
          }
          model.setViewName("login");
  
          return model;
  
      }
  
      //for 403 access denied page
      @RequestMapping(value = "/402", method = RequestMethod.GET)
      public ModelAndView accesssDenied() {
  
          ModelAndView model = new ModelAndView();
  
          //check if user is login
          Authentication auth = SecurityContextHolder.getContext().getAuthentication();
          if (!(auth instanceof AnonymousAuthenticationToken)) {
              UserDetails userDetail = (UserDetails) auth.getPrincipal();
              System.out.println(userDetail);
  
              model.addObject("username", userDetail.getUsername());
  
          }
  
          model.setViewName("402");
          return model;
  
      }
  
      @RequestMapping(value = "/Account/userManagement", method = RequestMethod.GET)
      public ModelAndView accountPage() {
          ModelAndView model = new ModelAndView();
          model.setViewName("Account/userManagement");
          return model;
      }
  
      @RequestMapping(value = "Notification/notification", method = RequestMethod.GET)
      public ModelAndView NotificationPage() {
          ModelAndView model = new ModelAndView();
          model.setViewName("Notification/notification");
          return model;
      }
  
      @RequestMapping(value = "test", method = RequestMethod.POST)
      public ModelAndView register(@ModelAttribute("user-entity") User user, BindingResult result)
      {
          ModelAndView model = new ModelAndView();
          UserDAO.create(user);
          model.setViewName("hello");
          return model;
      }
  
      @RequestMapping(value = {"/","/Account/registration"}, method = RequestMethod.GET)
      public ModelAndView registerPage()
      {
          ModelAndView model = new ModelAndView("/Account/registration", "user-entity", new User());
          return model;
      }
  
  }
  </code></pre>
  
  <p>this is my form code </p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;form:form action="test" method="POST" modelAttribute = "user-entity"&gt;
     &lt;td&gt; &lt;td&gt;&lt;form:label path="Username"&gt;Name:&lt;/form:label&gt;&lt;/td&gt;  
              &lt;td&gt;
              &lt;form:input path="Username"&gt;&lt;/form:input&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;Password:&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&lt;form:input type = "password" path = "Password" &gt;&lt;/form:input&gt;&lt;/td&gt;
            &lt;/tr&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>Exception</p>
  
  <blockquote>
    <p>Aug 03, 2014 6:12:53 PM org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
    WARNING: Request method 'POST' not supported</p>
  </blockquote>
  
  <p>This is a link to my previous question</p>
  
  <p><a href="https://stackoverflow.com/questions/25102518/post-not-working-in-spring-mvc-4">POST not working in spring mvc 4</a></p>
  
  <p>I modified my form action to </p>
  
  <pre><code> &lt;form:form action="/&lt;packagename&gt;/Account/test" method="POST" modelAttribute = "user-entity"&gt;
  </code></pre>
  
  <p>the default url of the registration page is </p>
  
  <pre><code>http://localhost:8080/&lt;package name&gt;/
  </code></pre>
  
  <p>and i updated my corresponding controller code to </p>
  
  <pre><code>@RequestMapping(value = "/&lt;package name&gt;/Account/test", method = RequestMethod.POST)
      public ModelAndView register(@ModelAttribute("user-entity") User user, BindingResult result)
      {
          ModelAndView model = new ModelAndView();
          UserDAO.create(user);
          model.setViewName("/Account/test");
          return model;
      }
  </code></pre>
  
  <p>The url i am trying to get to is /Account/test</p>