0

I need to make a Servlet which will manage some information and, after that, will go to a Liferay 6.2 Portlet. Both in the same server.

I need the Servlet to send a parameter, but I don't want to send it GET, but POST method. So, I try to put it in the session to retrieve it from the Portlet.

At the Servlet, I have:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        request.getSession().setAttribute("param1", "TEST 1");
        url = "http://myServer/";
        response.sendRedirect(response.encodeRedirectURL(url));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And at the Portlet I manage the information at render method, as I want to get param1 before I render the page:

public void render (RenderRequest renderRequest, RenderResponse renderResponse) 
        throws PortletException, IOException {
    super.render(renderRequest, renderResponse);
    //Try to retrieve from getOriginalServletRequest
    HttpServletRequest servletReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
    String param1 = servletReq.getSession().getAttribute("param1").toString();
    //Try to retrieve from getHttpServletRequest 
    HttpServletRequest servletReq_ = PortalUtil.getHttpServletRequest(renderRequest);
    String param1_ = servletReq_.getSession().getAttribute("param1").toString();
}

As you can see, I tried to retrieve from getHttpServletRequest and from getOriginalServletRequest, but I always get the param1 null.

Any suggestion? Thank you in advance!

Update question:

I'm being called from a third part, and I'm receiving a GET parameter I want to evaluate.

After that, and not rendering a page in the middle, I want to redirect to one or another Portlet, depending of that evaluation.

I need to send some personal information to those Portlets, so I want to send some parameters in POST method.

A Servlet doesn't fit as doesn't share session with Portlets.

I've tried to implement a landing Portlet, but the redirect can only be done in action phase, so I'd need to render a (empty) page before the redirect, don't like that part. Render phase doesn't allow redirect (even getting PortalUtil.getHttpServletResponse(), doesn't work)

Any suggestion? Thanks!

Marta
  • 97
  • 1
  • 1
  • 11
  • The edit doesn't make the situation clearer without reading all the comments under my answer (and even then I'm more confused than clear). I'd recommend to start another question with the full business requirements and what you've done so far. As far as I'm concerned, I'm considering the initial question answered and the update is a separate question. But please don't just copy/paste the portion, rather give a description of your requirements together with the code. – Olaf Kock Oct 18 '17 at 12:26

1 Answers1

2

A servlet and a portlet will not share the same session. The portlet is living within the portal server, e.g. Liferay. The servlet is typically in its own web application, thus completely separated by design.

If you need to communicate between the two, here are two possible solutions/workarounds:

  • reimplement your servlet as a portlet, potentially utilizing the resource-phase of a portlet
  • use a request parameter instead of a session attribute

Edit after all of the comments:

It seems best to take a step back and look at the underlying problem - what is the problem that you're actually trying to solve? The content of your question is how you're trying to solve it, and obviously there are challenges. It looks like the problem needs a different solution in the first place.

My answer describes why your solution can't work, but that obviously doesn't help solving the underlying problem.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Thanks. I suspected the session wouldn't be shared, but the method "getHttpServletRequest" gave me some hope :) I thought about the servlet being a portlet, I was just delaying this test because it's harder to implement in localhost because I'm working with single portlets and not a whole portal so I cannot create different pages to navigate one to another... – Marta Oct 16 '17 at 13:12
  • I tried to use request.setAttribute("param1", "TEST 1") but I don't get it neither. How can I set the request parameter easily? The request object doesn't allow a setParameter() call, and I've just read about building a wrapper... – Marta Oct 16 '17 at 13:13
  • I also tried with response.addCookie(), but it doesn't get to the portlet neither :( – Marta Oct 16 '17 at 13:14
  • request *parameter*, not attribute. You can construct a URL that has a parameter, not a request. – Olaf Kock Oct 16 '17 at 14:36
  • But that URL parameter would be sent as a GET call, something I wanted to avoid. Wouldn't it? I'm working in a portlet to portlet solution, let's see :) – Marta Oct 17 '17 at 06:27
  • I'm stuck because the first portlet just needs to evaluate some received info to redirect to the second portlet. I'd do it in render phase, but it's not allowed (as you explain in https://stackoverflow.com/questions/6878828/liferay-portlet-and-jsf-redirect-during-render-phase) I don't want to do it with javascript or any tricky way to render the jsp and redirect from there. Any suggestion? The PortalUtil.getHttpServletResponse(renderResponse) doesn't work neither... – Marta Oct 17 '17 at 07:10
  • Now you're speaking about two portlets, while your question contains a servlet and a portlet. I'm standing with my edit: Need to know the underlying problem, in business terms, not your intended solution. "need to evaluate some received info to redirect to the second portlet" is surely not an underlying problem, but part of the (incorrect) solution you envisioned. I consider this question answered, but you might want to ask another one stating your actual problem, or edit this one to do. – Olaf Kock Oct 17 '17 at 07:19
  • Well, I'm talking about two portlets because I was trying your solution: "reimplement your servlet as a portlet, potentially utilizing the resource-phase of a portlet" – Marta Oct 17 '17 at 07:37
  • But it's true that what I want is to go to a portlet, doesn't matter where I come from, so I'd change the original post – Marta Oct 17 '17 at 07:41
  • Yes, they have the same session, but I want redirect before render the page, and I don't know how to make that redirect before the action phase. – Marta Oct 17 '17 at 07:43