Hello friends
I have created a component which has few field in it and on the press of submit button i am calling ajax defined below :
$.ajax({
type: 'GET',
url:'/htlSearchServlet',
data:'id='+ claimId+'&firstName='+ myFirst+'&lastName='+ myLast+'&address='+ address+'&cat='+ cat+'&state='+ state+'&details='+ details+'&date='+ date+'&city='+ city,
success: function(msg){
var json = jQuery.parseJSON(msg);
var msgId= json.id;
var lastName = json.lastname;
var firstName = json.firstname;
$('#ClaimNum').val(msgId);
$('#json').val("Filed by " + firstName + " " + lastName);
}
});
and trying to call my servlet to handle these fields , below is the code for how i have defined my servlet to handle this request
@component(service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.resourceTypes="+ "/htlSearchServlet",
"sling.servlet.extensions=" + "txt"
})
/*"sling.servlet.resourceTypes="+ "appfoldername/components/structure/page",
* "sling.servlet.extensions=" + "txt",*/
public class SimpleServlet extends SlingAllMethodsServlet {
//code of Get request comes here
}
but don't know whats wrong i am doing, it is giving 404 not found error, when i checked in browser console using chrome browser.
Thanks in advance
Yash
Solved! Go to Solution.
Hi,
You have registered your servlet with resourceType, change to paths and extension can be remove as you are not calling with extenstion because your servlet will only we called for the url which has resourceType htlSearchServlet
Changed path and append /bin otherwise you have to do additional changes to make it work.
Try with below code
@Component(service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths="+ "/bin/htlSearchServlet"
})
$.ajax({
type: 'GET',
url:'/bin/htlSearchServlet',
Hi Yash,
In the code snippet you had mentioned, I see that you are trying to post some values to the servlet. Typically the "data" attribute in AJAX call is used to specify the data to be sent to the server.
So the first thing you need to do is change the method type: 'GET' to type: 'POST' in your AJAX call. Also change "sling.servlet.methods=" + HttpConstants.METHOD_GET in your servlet code to "sling.servlet.methods=" + HttpConstants.METHOD_POST
And also you should use sling.servlet.paths instead of sling.servlet.resourceTypes as shown in the below helpx article. However, if you'd like to use sling.servlet.resourceTypes, you need not make an AJAX call. Please refer the below articles which explain you regarding the different ways of registering the servlet.
https://hashimkhan.in/aem-adobecq5-code-templates/servlets/
AEM Developer Learning : Apache Sling : Servlets and Scripts in AEM 6.3
I'd recommend you to go the below helpx article. The aim of the below article is to submit form data to sling servlet and also it has an example showing how to write and AJAX call and submit the form data to sling servlet.
Adobe Experience Manager Help | Submitting Adobe Experience Manager form data to Java Sling Servlets
Please let me know in case if you still see the same issue even after following the steps mentioned in above article. I'll be more than happy to help you.
I hope this helps!
Regards,
Lavanya Malyala
Views
Replies
Total Likes
Hi,
You have registered your servlet with resourceType, change to paths and extension can be remove as you are not calling with extenstion because your servlet will only we called for the url which has resourceType htlSearchServlet
Changed path and append /bin otherwise you have to do additional changes to make it work.
Try with below code
@Component(service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths="+ "/bin/htlSearchServlet"
})
$.ajax({
type: 'GET',
url:'/bin/htlSearchServlet',
Excellent responses - they are correct.
Views
Replies
Total Likes
In addition - see this AEM article - that shows an AEM Sling Servlet using OSGi R6 annotations -- Creating a Mail List Sign Up Component for the Experience Manager Toy Store Site
Views
Replies
Total Likes
@ Arun Patidar
I have tried the way you have asked me to change but it didn't work , i am working on AEM 6.4 , please have a look on my changed code :
@Component(service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.path="+ "/bin/htlSearchServlet"
})
public class SimpleServlet extends SlingAllMethodsServlet {//SlingSafeMethodsServlet
private static final long serialVersionUid = 1L;
Logger log = LoggerFactory.getLogger(SimpleServlet.class);
@Override
protected void doGet(final SlingHttpServletRequest request,
final SlingHttpServletResponse resp) throws ServletException, IOException {
log.info("doGet called in simple servlet");
final Resource resource = request.getResource();
resp.setContentType("text/plain");
resp.getWriter().write("Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));
}
}
I am expecting log in my log file under crx-quickstart/logs/filename (correspond to my packname under log support).
OR
print the title on my web page.
changed ajax code :
$.ajax({
type: 'GET',
url:'/bin/htlSearchServlet',
data:'id='+ claimId+'&firstName='+ myFirst+'&lastName='+ myLast+'&address='+ address+'&cat='+ cat+'&state='+ state+'&details='+ details+'&date='+ date+'&city='+ city,
success: function(msg){
alert("ajax success. ");
var json = jQuery.parseJSON(msg);
var msgId= json.id;
var lastName = json.lastname;
var firstName = json.firstname;
$('#ClaimNum').val(msgId);
$('#json').val("Filed by " + firstName + " " + lastName);
}
});
Views
Replies
Total Likes
Hi,
Can you try to hit your servlet directly in browser, the see what you are getting and please share your import as well.
<server_name>:<port>/bin/htlSearchServlet
Views
Replies
Total Likes
Please see the screen shot below :
also which imports you need, please tell.
Views
Replies
Total Likes
Hi,
Your servlet is not registered properly.
check sample servlet at aem63app-repo/SimpleGetGroup.java at master · arunpatidar02/aem63app-repo · GitHub
There could be issue with java imports, can you try above code and see if you can able to call servlet.
Views
Replies
Total Likes
Views
Likes
Replies