Skip to main content

jmaki.DoAjax fails in Netbeans portlet

16 replies [Last post]
dwhite
Offline
Joined: 2008-06-16
Points: 0

Hello,

I have created a simple JSR-168 portlet (via Netbeans portalpack plugin) with a jmaki Yahoo tree and a Dojo Table. When a tree node is selected the table is re-loaded with the data relevant to the selected node.

I am using the glue.js file to add rows to the table as follows.

jmaki.subscribe("/table/clear", function(args) {
var message = args.message.targetId;
jmaki.doAjax({method: "POST",
url: "Service?message=" + encodeURIComponent(message),
callback: function(req) {
objects = eval(req.responseText);
jmaki.publish("/table/addRows",{value: objects});
}
});
});

I have created a servlet called 'Service' but when I select a tree node I receive the following error:

"jMaki.doAjax Error: doAjax error communicating with Service?message=155. Server returned status code 404"

The code works fine when I run it as a standard web application (i.e. not within a portlet) and the web.xml file seems to look ok. I even tried adding 'requireSession' context parameter to the web.xml but still no joy.

I would be really grateful if anyone has any suggestions or workarounds

Thanks very much in advance

David

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dwhite
Offline
Joined: 2008-06-16
Points: 0

Just in case anyone is interested, the following relative path works as well...

url: "../../TAPortlet01/Service?message=" + encodeURIComponent(message)

marcfarrow
Offline
Joined: 2008-06-19
Points: 0

This is a setup issue with your container/web pages and not a problem with jMaki then. Without looking at your entire web app setup, I can't be of much help.

Sorry.

dwhite
Offline
Joined: 2008-06-16
Points: 0

Thanks for the test link - I tried using this link in my code and the servlet was called correctly and returned the desired result.

i.e.
url: "http://localhost:8082/TAPortlet01/Service?message=" + encodeURIComponent(message)

I really need to get a relative path in there - but for now at least I know that the servlet works and I can call it (and I can get my sanity back).

Thanks for your help - much appreciated.

dwhite
Offline
Joined: 2008-06-16
Points: 0

Thanks for your help - I appreciate it...

I tried adding the ?message=test message

but it does not write anything to the page - just completely blank...

dwhite
Offline
Joined: 2008-06-16
Points: 0

I do have a META-INF folder but there is just one MANIFEST.MF file in it.

I tried the link you sent me and it does not produce an error but just a blank page (if I change the name of the servlet to something different i.e. ServiceXXX then I get an error).

Out of interest - the code in my servlet is as follows ( I have only implemented the doPost method)

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HttpSession session = request.getSession();
//Tasks tasks = (Tasks)session.getAttribute("tasks");

PrintWriter out = response.getWriter();

GridObject object = new GridObject();
String taskId = request.getParameter("message");

try {

JSONArray array = object.getObjectsJSON(taskId);

out.println(array.toString());

} catch (Exception e) {
out.println(e);
} finally {
out.close();
}
}

Thanks

marcfarrow
Offline
Joined: 2008-06-19
Points: 0

Take the test link add your message parameter to it:

http://localhost:9999/TAPortlet01/Service?message=TEST_MESSAGE

You should see the test message on the "blank" page.

dwhite
Offline
Joined: 2008-06-16
Points: 0

here is my web.xml. (I couldn't find a content.xml file). I tried:

/TAPortlet01/Service




Switch off caching. You can remove this parameter or comment it out when this app goes into production.

jmaki-usecache false

requireSession false


XmlHttpProxy
jmaki.xhp.XmlHttpProxyServlet


Service2
service.Service2


Service
com.test.Service


XmlHttpProxy
/xhp


Service2
/Service2


Service
/Service



30



index.jsp

marcfarrow
Offline
Joined: 2008-06-19
Points: 0

Do you have a META-INF folder?

Does this link work:

http://localhost:9999/TAPortlet01/Service

You may have to change out "localhost" with your "IP or machinename".

You will need to replace "9999" with the port that your glassfish is running on.

If you can get that link to work, then you have the servlet set up properly. Since you are getting 404 error, I think either you don't have servlet set up properly or your connection cannot reach application through "port" specified. We have Apache's HTTP server in front of all our apps and we push everything through Apache and then forward the request to Tomcat if needed (this is done with configuration files and mod_jk2). If you are not using another HTTP server, then what I listed above would be a good place to start. If you are using one, then you will have to adjust your testing to meet those requirements.

dwhite
Offline
Joined: 2008-06-16
Points: 0

The problem is that I am writing a portlet (using Net beans) and deploying it using Openportal Portlet Container 2.0. I think this uses the glassfish app server.

I have not deployed the servlet explicitly, it is just contained in the same project as the portlet.

I did the same when deploying my standard web application (i.e. I did not deploy the servlet explicitly - it was just part of my web app project) and this worked (glassfish app server).

I guess I must need to do something extra to deploy/use servlets within the openportal Portlet Container.

Thanks

marcfarrow
Offline
Joined: 2008-06-19
Points: 0

Deploying your portlet that contains the servlet should be suffice. Can you post your web.xml and context.xml (if it exists). (please "hide" any sensitive information. I am not sure how Glassfish works, but it should be a standard web container.

Have you tried:

url : '/portletProjectName/Service'

?

dwhite
Offline
Joined: 2008-06-16
Points: 0

Thanks again for your advice. I think that you're probably right about the context of the URL mapping being incorrect. Unfortunately I have not been able to determine what the correct 'context' should be as yet.

If anyone has successfully been able to call a servlet from a portlet using jMaki.DoAjax I would be very keen to know what URL they used.

Thanks

David

marcfarrow
Offline
Joined: 2008-06-19
Points: 0

The context should be whatever project the snippet of the web.xml file is in that you posted earlier. What servlet container are you using? If Tomcat, then where is the file deployed? There should be context descriptor that will give you the context.

dwhite
Offline
Joined: 2008-06-16
Points: 0

Thanks for your quick reply. The servlet elements from the web.xml are here


XmlHttpProxy
jmaki.xhp.XmlHttpProxyServlet


Service2
service.Service2


Service
com.test.Service


XmlHttpProxy
/xhp


Service2
/Service2


Service
/Service

Sorry - but what did you mean when you ask if the 'Service' is mapped relative to the current application?

I tried calling DoAjax with JSON object for request parameters but still the same error.

Thanks again for your help - I wonder if there is another way to call my servlet without using jmaki?

marcfarrow
Offline
Joined: 2008-06-19
Points: 0

(sorry for previous double post.. the java.net site seemed to be hanging).

When you are making an http request from a page, you have three basic ways of retrieving data.

1) Absolute URL
a) http://www.hostname.com/mywebapp/Service

2) Relative URL
a) "index.jsp"

3) Context relative (I just made this term up)
a) "/mywebapp/Service"
b) Basically this is saying I want to make a request from the same server.
c) However, you are specifying which context you want to access.

(The above list may not be all inclusive and also it is more complicated than this; but this is just meant to give you a nudge in the right direction. You will really need to web application specifications to get the full details).

In your URL mapping for your web.xml you have com.test.Service mapped to "/Service", but you are accessing it via "Service" only. The culprit could be the lack of a leading "/". When you add the "/", it causes the request to fall into category 3. What I am not sure of is whether or not you need to include the "context" name with the URL. I always use the context name, but I am annal like that I want to be 100% clear about where I am making the request to. So try the url with just "/Service" and if that doesn't work, then try it with "/contextname/Service"

hth.

marcfarrow
Offline
Joined: 2008-06-19
Points: 0

A few of things:

1) Check to make sure your servlet-mapping tag in your web.xml states "Service" (matching case and all)

2) Is the "Service" mapped relative to the current application? You can also try using

url: "/contextName/Service"

3) Also, jMaki allows the use of a JSON object to represent request parameters:
(What you have should work though)

jmaki.subscribe("/table/clear", function(args) {
var message = args.message.targetId;
var requestParameters = {
message : encodeURIComponent(message);
}
jmaki.doAjax({method: "POST",
url: "Service',
content : requestParameters,
callback: function(req) {
objects = eval(req.responseText);
jmaki.publish("/table/addRows",{value: objects});
}
});
});

marcfarrow
Offline
Joined: 2008-06-19
Points: 0

A few of things:

1) Check to make sure your servlet-mapping tag in your web.xml states "Servlet" (matching case and all)

2) Is the "Servlet" mapped relative to the current application? You can also try using

url: "/contextName/Servlet"

3) Also, jMaki allows the use of a JSON object to represent request parameters:
(What you have should work though)

jmaki.subscribe("/table/clear", function(args) {
var message = args.message.targetId;
var requestParameters = {
message : encodeURIComponent(message);
}
jmaki.doAjax({method: "POST",
url: "Service',
content : requestParameters,
callback: function(req) {
objects = eval(req.responseText);
jmaki.publish("/table/addRows",{value: objects});
}
});
});