0
votes

There is a database server and a portal with installed portlet. Portlet represents a view, divied to two parts. At the left side there is a navigation tree. When user clicks on tree nodes he gets information from the database server and portlet should display this information on the right side of the view.

On my assumption portlet view page contains javascript code, when user clicks on a tree node, then portlet sends request to the servlet, gets servlet response, parses it and updates the view.

Portlet view layout is displayed below:

If it were only servlet based application, then I would implement javascript calls from the page to the server, return json, parse it on the page and update the page representation.

But java portlets are a bit different and I did not find a way how to do that.

And my question is there a way to package a servlet with portlet in one *.war file and implement requests from portlet to this servlet. Portlet sends all requests to this servlet, then servlet accesses database server, and after that sends results to the portlet.

If the answer is "yes" - how can I do that. Because I tried to package servlet with portlet, but such portlet does not work.

If the answer is "no" - what is the best approach to implement this "data query->response->update view" sequence in a portlet?

Thank you.

2
Yes, that will work, but "such portlet does not work." is not enough information to go on.McDowell
Thank you. I will provide more details a bit later. Now I am leaving the office. Later I will update my post with information about the error. Thanks again.user784540
I have resolved the situation and posted the response here. Thank you :)user784540

2 Answers

0
votes

Glad that you have answer to what you were looking to do but I think it may be simpler and cleaner to use the serveResource method from the portlet API to do this. One of the main reasons the serveResource method was added in the portlet 2.0 spec was to make AJAX functionality simpler in portlets.

0
votes

Reason of my non-working servlet+portlet combination was incorrect path to the servlet specified in portlet view jsp.

Now the task is resolved and below there are details. I think it will help someone who faced the same problem.

Assume, we have a servlet GetData which does the following:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/plain;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("Hello there! Привееееет!");
    } finally {            
        out.close();
    }
}

Below there is code for TestPortlet_view.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script>
  function loadData() {
    $.get('<%=request.getContextPath()%>' + '/GetData', function(data) {
        alert(data);
    });    
  }
  </script> 
 <input type="button" value="Hit me" onClick="javascript:loadData();"/>

Below, there's picture of the portlet when it has been deployed on the portal.

And when we click the button:

Voila, it works :)

Special thanks to McDowell for heads up.