0

i don't know if i asked the right question but anyway that is my problem : i followed a tutorial showing how to use ajax with Java ee and i made a simple example and it worked, but i tried to apply what i learned in my project and it didnt work, here is my jsp file :

<div class="aimerSection">
     <div class="aime">
     <form>
     <input type="hidden" class="adore1" name="aime" value="${post.id}">
     <input type="button" class="adore2" value="J'aime">
     </form>
     </div>
     <c:if test="${ post.adore == 0 || post.adore == 1 }">
     <div class="nbreAimes"><p><span class="nbrAdore">${ post.adore }</span> personne aime ça</p></div>
     </c:if>

     <c:if test="${ post.adore != 0 && post.adore != 1 }">
     <div class="nbreAimes"><p><span class="nbrAdore">${ post.adore }</span> personnes aiment ça</p></div>
     </c:if>

</div>

What i wanted is to get the value in the hidden input by clicking on my button( input button) so here is my jquery file :

    $(".adore2").click(function(){
    var aime = $(this).parent().find(".adore1").val()
    var value=$(this).parent().parent().siblings().find(".nbrAdore").text()
    alert(value)

    $.ajax({
        type:"POST",
        data: {aime:aime},
        url:"acceuilServlet",
        success:function(result){
             $(this).parent().parent().siblings().find(".nbrAdore").html(result)
        }
    })

})

And i wanted to see the result in the console ( as a test ) so here is a part of my Controller ( doPost method ) but i am getting any response.

    resp.setContentType("text/plain");

    int aime = Integer.parseInt(req.getParameter("aime"));
    aime++;
    System.out.println("hello "+aime);

Any help would be much appreciated.

TaouBen
  • 1,165
  • 1
  • 15
  • 41

1 Answers1

2

first you need to register your servlet in Web.xml like below. Then it knows to accept the requests.

<web-app>
      <servlet>
        <servlet-name>yourServlet</servlet-name>
        <servlet-class>com.java.YourServlet</servlet-class>
      </servlet>

      <servlet-mapping>
        <servlet-name>yourServlet</servlet-name>
        <url-pattern>*</url-pattern>
      </servlet-mapping>
    </web-app>    
  • 1
    Hello! thank you Sir i added an annotation @WebServle("/MySerlet") and worked, thank for reminding me of this .. have a blessed day. – TaouBen Dec 16 '17 at 00:58
  • 1
    Welcome.... You too the same –  Dec 16 '17 at 00:59