How to retrieve the session of the original request to use session.setAttribute(...) in a java action?

4
Hello, I have a big problem showing images in a browser via JasperReports because I have to set an attribute in the session of the original request object org.eclipse.jetty.server.Request. This looks like: request.getSession().setAttribute(ImageServlet.DEFAULTJASPERPRINTSESSION ATTRIBUTE, jasperPrint); Unfortunately, retrieving the object Session by request.getSession() via an original request instance results in Null. Is this a known bug or am I doing something wrong? Added: To be a bit more precise: My problem isn't about having difficulties in setting up a servlet. I have to change the attribute of the Session object of a servlet which is Null. The link between my ImageServlet and my ReportServlet is broken. I need the ImageServlet to translate the image-identifiers generated by JasperReports (i.e. img00_2) to "normal" names (i.e. normalimagename.jpg). Added: I'm registering the servlet via the Core.addRequestHandler method. The servlet works without the HTML report generation perfectly. That means I can download generated reports as pdf, xls, docx etc.. The only problem is that Mendix doesn't register the http session correctly and I can't get any images in my browser because the names can't be translated. The translation sequence is afaik so: - Client makes request - Server gets request in MainServlet - MainServlet calls JasperReports - JasperReports translates image names - Image names are registered in ImageServlet (inheriting HttpServlet) - Result is shown in browser: Names of image paths are retrieved by ImageServlet which is called by the MainServlet As you can see, I need a session object to identify the client session and set/register the printable JasperReports object for that session. The session of the requesthandler child object is probably Null, because the Core.addRequestHandler function doesn't give the original jetty session of the Mendix RequestHandler parent to its children. I'm using Mendix 2.5.0.1 and higher. Which version is supporting http sessions or does anybody know if such a version is at least available soon? Thanks, Michael Walker
asked
3 answers
3

If I understand you correctly, you're calling the addrequesthandler method on Core, which doesn't take servlets as a method parameter but "RequestHandler"s. These are a mendix-specific emulation of servlets, but don't actually support any of the methods that servlets themselves support.

Anyway, proceeding from there, I'm assuming all the classes you mentioned aren't servlets but instead RequestHandlers. This makes it a bit interesting, because that means that you're working in the "m2ee context" instead of the "j2ee context", if you understand what I mean. In the mx runtime, we work with ISession objects instead of http session objects. every RequestHandler inherits from the base abstract class "RequestHandler", which has a method

getSessionFromRequest(IMxRuntimeRequest request)

This in return gives you an ISession object, which has a UUID that can be used to uniquely identify the session. (an alternative to identify requests is getContext().getRequestId(), but that won't help you if you want to identify sessions as opposed to requests)

So, one option you could go for is implementing your own sessionmanager (some type of singleton) and registering objects there, under the session UUID.

(On a side note: it's really cool to see people using these APIs. Although this is the way we planned it, it's kinda techy stuff that normal business users might find a bit too complicated. We're open for suggestions on how to expand this API (such as being able to register parameters or whatever in the ISession object) so hit us up with some feature requests in MXDN)

answered
1

You can write your own servlets and attach them to the webserver using

Core.addRequestHandler("yoursuburl/", new YourHandlerClass());

If you implement this handler class you should be able to access any property of the Request/ Response objects which are passed to the servlet.

answered
1

The problem is solved. I've extended the JasperReports component and have overwritten the related functions to make the j2ee-troublemakers Mendix-compatible. That means: Replacing everything related to BaseHttpServlet etc. with global objects and add getters/setters to modify the properties.

answered