0

Before anyone tries to mark this as duplicated or mark down because of a lack of research I acknowledge this question already exists on stack overflow but the offered solutions do not solve my problem so I wanted to see if people could solve this unique problem I am experiencing.

this is my form

<form:form method="POST" action="addQuestion" >

   <input type="text" name="questionId" />Enter Id<br>
   <input type="text" name="theQuestion" />Enter Q <br>
   <input type="text" name="category" />Enter Category<br>
   <input type="text" name="correctAnswer" />Enter correct answer<br>
   <input type="submit" value="Next"  >

</form:form>

and this appears in my web.xml

<servlet>
        <servlet-name>addQ</servlet-name>
        <servlet-class>main.WebController</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>addQ</servlet-name>
        <url-pattern>/addQuestion</url-pattern>
    </servlet-mapping>

and this is my webcontroller

@RequestMapping("/addQuestion")
        public String addQuestion(ModelMap model, @RequestParam(value="question", required = true)  String theQuestion , @RequestParam(value="questionId", required = true)  Integer questionId, @RequestParam(value="category", required = true)   String category, @RequestParam(value="correctAnswer", required = true)   String correctAnswer) throws SQLException{
            ViewController viewController = new ViewController();
            viewController.createQuestion(questionId, theQuestion, category, correctAnswer);
            model.addAttribute("message", "Hello hope this flipping works");

        return "addQuestion";
    }

and the error message I am getting is HTTP method POST is not supported by this URL

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
BlueShark
  • 150
  • 5
  • 7
  • 20
  • Do you have doPost method in your servlet? It's the default implementation of doPost() that throws the error saying the method is not supported. – Semih Eker Nov 26 '14 at 09:54
  • where should my doPost method actually go and what does it do differently do my methods? – BlueShark Nov 26 '14 at 09:56
  • You should use doGet() when you want to intercept on HTTP GET requests. You should use doPost() when you want to intercept on HTTP POST requests. Please look this question; http://stackoverflow.com/questions/2349633/doget-and-dopost-in-servlets – Semih Eker Nov 26 '14 at 10:10
  • @SemihEker The raw servlet handler methods have nothing to do with this question, which is a Spring MVC issue. – chrylis -cautiouslyoptimistic- Nov 26 '14 at 10:19
  • @BlueShark You seem to be heavily mixing Spring MVC classes with raw Servlet `web.xml` configuration. This still wouldn't work for other reasons, but I'm skeptical that your controller is getting loaded at all. – chrylis -cautiouslyoptimistic- Nov 26 '14 at 10:21
  • @chrylis i think i am. im still getting confused as how to do what i want to do without using servlets. a tutorial on this would be helpful but i cant see seem to find one. – BlueShark Nov 26 '14 at 10:39
  • For starters: Use Spring Boot. Let it handle all the server wiring and just pay attention to the controller logic for now. – chrylis -cautiouslyoptimistic- Nov 26 '14 at 11:16

1 Answers1

1

Do this:

@RequestMapping(value="/addQuestion", method=RequestMethod.POST)

ArinCool
  • 1,720
  • 1
  • 13
  • 24