Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Servlet cannot be correctly instantiated by the Use API

Avatar

Avatar
Validate 1
Level 1
mariaanto
Level 1

Like

1 like

Total Posts

10 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
mariaanto
Level 1

Like

1 like

Total Posts

10 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
mariaanto
Level 1

09-05-2016

Hi,

 

I am trying to achieve, sling servlet application using sightly.

But, it is showing “org.apache.sling.scripting.sightly.SightlyException: Identifier com.ap.website.cssurvey.core.servlets.SurveyServlet cannot be correctly instantiated by the Use API”

Kindly help me in resolving this issue.

 

Regards,

Maria Anto

------------------------------------------------------------------

Sightly code:

<!DOCTYPE html>
<html>
    <sly data-sly-use.css="${'com.ap.website.cssurvey.core.servlets.SurveyServlet'}" data-sly-unwrap>
    <sly data-sly-use.clientLib="/libs/granite/sightly/templates/clientlib.html" data-sly-call="${clientLib.js @ categories='jquerysamples'}" data-sly-unwrap></sly> 
    <head>
        <title>Survey Application</title>
        <script>  
        $(document).ready(function() {
            alert("Click");
            $('body').hide().fadeIn(5000);

            $('#submit').click(function() {
            alert("Submit");
                var failure = function(err) {
                    alert("Unable to retrive data "+err);
                };


                //Get the user-defined values that represent claim data to persist in the Adobe CQ JCR
                var qu1= $('#ques1').val() ; 
                var qu2= $('#ques2').val() ; 
                var qu3= $('#rating').val() ; 

                var resourcepath = $('#resourcepath').val();
                alert(resourcepath);

                alert(qu1);    
                alert(qu2);    


                //Use JQuery AJAX request to post data to a Sling Servlet
                $.ajax({
                    type: 'POST',    
                    url: resourcepath +'.slingServlet.json',
                    data:'ques1='+ qu1+'&ques2='+ qu2+'&rating='+ qu3,
                    success: function(msg){
                        var json = jQuery.parseJSON(msg);           
                        var n = json.Q1;
                            alert(n);
                        $('#sub').val("Details updated !!"); 
                        }
                 });
            });
            
        }); // end ready
        </script> 
    </head>
    <body>
    <input type="hidden" value="${resource.path}" id="resourcepath"/>  
        <form method="#">
            <table border="0" align="left">
                <tr>
                    <td>
                        <label for="ques1">1. How satisfied are you with the outcome / resolution of the complaint ?</label>
                    </td>
                    <td>
                        <input type="text" id="ques1" name="ques1" maxlength="10">
                    </td>
                </tr> 
                <tr>
                    <td>
                        <label for="ques2">2. How satisfied are you with the time taken to give you a feedback / solution to your complaint ?</label>
                    </td>
                    <td>
                        <input type="text" id="ques2" name="ques1" maxlength="10">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="ques3">3. How satisfied are you with the responsiveness of the complaint registered by you ?</label>
                    </td>
                    <td>
                        <select id="rating" name="ratingVal">
                            <option value="rat5">Rating 5</option>
                            <option value="rat4">Rating 4</option>
                            <option value="rat3">Rating 3</option>
                            <option value="rat2">Rating 2</option>
                            <option value="rat1">Rating 1</option>
                            <option value="NA">NA</option>
                        </select>
                    </td>
                </tr> 
                <tr>
                    <td>
                        <label for="ques4" id="ques4">4. Have you received Compensation ?</label>
                    </td>
                    <td>
                        <input type="radio" name="yes" value="yes"> Yes  <input type="radio" name="no" value="no"> No
                    </td>
                </tr>
                <tr>
                    <td>
                        <label id="sub">Abc</label>
                    </td>
                    <td>
                        <input type="button" value="Submit" name="submit" id="submit">
                    </td>
                </tr>                 
            </table>            
        </form>
    </body>
    </sly> 
</html>

---------------------------------------------------------------------------------

Servlet code:

package com.ap.website.cssurvey.core.servlets;

import java.io.IOException;
import java.rmi.ServerException;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.jcr.api.SlingRepository;
import org.json.simple.JSONObject;

@SlingServlet(
resourceTypes = "cssurvey/components/content/css", 
selectors = "slingServlet",
extensions = "json",
methods = "POST")

public class SurveyServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet{
    private static final long serialVersionUID = 2598426539166789515L;
    
    @Reference
    private SlingRepository repository;
     
    public void bindRepository(SlingRepository repository) {
           this.repository = repository; 
           }
          
    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
      
   
     try
     {
         String Qn1 = request.getParameter("question1");
         String Qn2 = request.getParameter("question2");
         String Qn3 = request.getParameter("rating");
         
        /* 
         System.out.println("ques1: "+ques1);
         System.out.println("ques2: "+ques2);
         System.out.println("rating: "+ques3);
         
         */
         
         //Encode the submitted form data to JSON
         JSONObject obj=new JSONObject();
         //obj.put("id",id);
         obj.put("Q1",Qn1);
         obj.put("Q2",Qn2);
         obj.put("Rank",Qn3);
         
          
            //Get the JSON formatted data    
         String jsonData = obj.toJSONString();
          
            //Return the JSON formatted data
        response.getWriter().write(jsonData);
     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
   }
}