'How to edit data in database using JPA?

I have a jsp form (candEditQues) that reads DB content and the submit button runs a jpa method (/editAnswer) to update the DB content. However, when I click the submit button I get the

Error 405 Method Not Allowed.

what am I doing wrong?

CndEditQues.jsp

<h1>Candidate Questions Form</h1>
<br>
<form action='/rest/service/editAnswer' method='post'>
    <%-- <input type="text" name="candidate_id"
        value="${sessionScope.LoggedUser.candidate_id}"> --%>
    <table>
        <tr>
            <td>CandidateID</td>
            <td>QuestionsID</td>
            <td>Answers</td>


        </tr>
        <c:forEach var="Vastaus" items="${requestScope.Vastauslista}">
            <tr>
                <td><input type="text" name="candidate_id"
                    value="${sessionScope.LoggedUser.candidate_id}" hidden>${sessionScope.LoggedUser.candidate_id}</td>
                <td><input type="text" name="id${Vastaus.id}"
                    value="${Vastaus.id}" hidden>${Vastaus.id}</td>
                <td></td>
                <td>${Vastaus.vastaus}</td>
                <td>${Vastaus.kommentti}</td>
                <td></td>
                <td><input type="radio" id="q1${Vastaus.id}"
                    name="vastaus${Vastaus.id}" value="1"> <label
                    for="q1${Vastaus.id}">1</label><br></td>

                <td><input type="radio" id="q2${Vastaus.id}"
                    name="vastaus${Vastaus.id}" value="2"> <label
                    for="q2${Vastaus.id}">2</label><br></td>

                <td><input type="radio" id="q3${Vastaus.id}"
                    name="vastaus${Vastaus.id}" value="3"> <label
                    for="q3${Vastaus.id}">3</label><br></td>

                <td><input type="radio" id="q4${Vastaus.id}"
                    name="vastaus${Vastaus.id}" value="4"> <label
                    for="q4${Vastaus.id}">4</label><br></td>

                <td><input type="radio" id="q5${Vastaus.id}"
                    name="vastaus${Vastaus.id}" value="5"> <label
                    for="q5${Vastaus.id}">5</label><br></td>

                <td><input type="text" id="kommentti${Vastaus.id}"
                    name="kommentti${Vastaus.id}" placeholder="Add an Explanation">
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </c:forEach>
    </table>

    <br> <br>
    <table>
        <tr>
            <td><input type='submit' name='ok' value='Update'
                style="font-size: 30px"></td>
            <td><input style="font-size: 30px;" type='reset'
                name='reset' value='Reset' id="button2"></td>
            <td><input style='font-size: 30px' type='button'
                name='cancel' value='Cancel' onclick='window.history.back()'></td>

        </tr>
    </table>
</form>

Service.java:

..code
@PUT
    @Path("/editAnswer")
//  @Produces(MediaType.TEXT_HTML)
//  @Produces(MediaType.APPLICATION_JSON)
//  @Consumes(MediaType.APPLICATION_JSON)
    @Consumes("application/x-www-form-urlencoded")
    public void editAnswer(MultivaluedMap<String, String> fp, @Context HttpServletRequest request,
            @Context HttpServletResponse response) throws IOException, ServletException {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("electionMachine");
        EntityManager em = emf.createEntityManager();

        try {

            for (int i = 1; i < fp.keySet().size(); i++) {

                int vastaus = Integer.parseInt(fp.getFirst("vastaus" + i));
                String kommentti = fp.getFirst("kommentti" + i);

                int candidate_id = Integer.parseInt(fp.getFirst("candidate_id"));
                int kysymys_ID = Integer.parseInt(fp.getFirst("kysymys_ID" + i));

                System.out.println("     " + kommentti + "    " + vastaus + "   " + candidate_id + "    " + kysymys_ID);

                Vastaukset vas = new Vastaukset(kommentti, vastaus, candidate_id, kysymys_ID);

                Candidate ca = new Candidate();
                ca.setCandidate_id(candidate_id);
                vas.setCandidate(ca);

                Kysymykset k = new Kysymykset();
                k.setKysymys_ID(kysymys_ID);
                vas.setKysymykset(k);

                em.getTransaction().begin();
                ca = em.find(Candidate.class, candidate_id);
                k = em.find(Kysymykset.class, kysymys_ID);
                vas = em.find(Vastaukset.class, kommentti);
                vas = em.find(Vastaukset.class, vastaus);
                vas = em.find(Vastaukset.class, candidate_id);
                vas = em.find(Vastaukset.class, kysymys_ID);

                System.out.println("check 1");
                if (vas != null) {

                    em.merge(vas);
                    System.out.println("check 2");
                    em.getTransaction().commit();
                    System.out.println("check final");
                }

            }
            em.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

If more code is needed to help find the problem please let me know. And please bare with me as I am a beginner.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source