0

i have implemented different solution of this problem but i am unable to access the controller method from jsp some solution that i have used are following

<script type="text/javascript">
var entryId = arguments[0]; 
function callMetod()
    {
   <%!public void clickBtn() {
    ControllerJSP controller = new ControllerJSP();
    controller.likePicture();
    }%>

    }
    </script>

solution 2 is

<script type="text/javascript">
var entryId = arguments[0]; 
function callMetod()
{
    var entryId = arguments[0]
    $.post( "../controllerJSP/LikePicture", {eid: 232 })
    .done(function( data ) {
    alert( "Data Loaded: " + data );
 });
 </script>

Solution three that i can implemented is

 <script type="text/javascript">
 function callMetod()
 {
      `
         var f={};
                var response = "4292";              
                f.url = "../controllerJSP/LikePicture";
                f.type = "POST";
                f.dataType = "json";
                f.data={eId:entryId};
                f.contentType = "application/json";
                f.success = function (entryId) {
                    console.log(json)

                    alert("success");
                };

                f.error = function (){
                    alert("failed");
                };

                $.ajax(f);
                alert("run")
 }
 </script>

please help me to resolve this issue

3
  • solution 1 is bad. What is your problem? What is happening? any error logs ? We need more than what you have to help you. Commented Oct 22, 2018 at 13:47
  • Solution 1 is something which you won't like to go with. For solution 2 and 3 need to see how you are defining handler in ControllerJSP.java? Are you able to call the service through postman? Commented Oct 22, 2018 at 14:19
  • i want to call a java controller method in a jsp page and i would not want to going any other page. Commented Oct 22, 2018 at 16:11

1 Answer 1

1

i will find a solution to call the method inside jsp from controller put an annotation requestmapping and assign a name as shown below and call this annotation in your ajax url like shown below

     @RequestMapping("likePicture")
     @ResponseBody
     public void likePicture(@RequestParam("eId") String eId, Map<String, Object>map)
     {
          List<LikeCount> likeCounts = new ArrayList<>();
          likeCounts = dal.getLikeCount(eId);
          SimpleResponseModel srm = new SimpleResponseModel();
          srm = dal.likePicture(eId, "[email protected]");
          dal.updateLikeCount(srm.getMessage(), eId);
          System.out.println(srm.getMessage());
      }

ajax method is shown here

    function likeImg(id, votes) 
    {
        var btn = document.getElementById("likeBtn");
        debugger
        $.ajax({

            url : "../likePicture",
            type : "GET",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data : {eId:id, vote:votes},
            success : function(responce) {
                alert("ok")
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.