0

I will appreciate if someone help me with the following problem. I have a jasper report which i fill in a PrintingBean and its all good. The moment I clicked on a print preview button (opening the applet) my app throws a null pointer exception at:

if (bean.getPrintingDataList() != null && !bean.getPrintingDataList().isEmpty())

It seems like it makes new session (but I can't see that on gui, its all good). My manageBean is a SessionScoped. This is my whole method:

private void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    JasperPrint jasperPrint = null;

    try {
        PrintingBean bean = (PrintingBean) request.getSession().getAttribute("printMB");
        if (bean.getPrintingDataList() != null && !bean.getPrintingDataList().isEmpty()) {
            jasperPrint = printManager.print(bean.getPrintingDataList());
        }
    } catch (Exception ex) {
        Logger.getLogger(JasperPrintServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (jasperPrint != null) {
        response.setContentType("application/octet-stream");
        ServletOutputStream ouputStream = response.getOutputStream();

        ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
        oos.writeObject(jasperPrint);
        oos.flush();
        oos.close();

        ouputStream.flush();
        ouputStream.close();
    }
}
falconiki
  • 3
  • 1
  • 5
  • 1
    That's impossible. Don't you *actually* mean that it returns `null` on the given line and that the `NullPointerException` is *actually* thrown at the `if` block thereafter where you access the `bean`? That would make much more sense and that *can* be answered. – BalusC Feb 06 '12 at 14:05
  • Yes, you are right it throws NPE at the if block because request.getSession() has no attributes (size is 0) and .getAttribute("printMB") does not exist at all. Do you have any idea why and how to handle this problem? Thanks for your time – falconiki Feb 07 '12 at 13:36

2 Answers2

1

The session is maintained by a cookie with the name JSESSIONID. Normally, this cookie is set by the server on start of session and this cookie is returned back from client to server on every subsequent single HTTP request throughout the session. The client (the webbrowser) does this all transparently. See also How do servlets work? Instantiation, sessions, shared variables and multithreading.

In the applet you need to simulate the same as the webbrowser is doing. When the applet connects to the servlet and needs to access the same session as the page which is serving the applet, then you should make sure that you append the very same session cookie to the HTTP request which is been sent by the applet.

The easiest is to pass the session ID as a parameter to the applet:

<param name="JSESSIONID" value="#{session.id}">

(note: I'm assuming that you're using Facelets as view technology, if you were using JSP, then you should use ${pageContext.session.id} instead)

So that you can set the needed session cookie in the applet accordingly:

String jSessionID = getParameter("JSESSIONID");
URL servletURL = new URL(getCodeBase(), "yourServletURL");
URLConnection connection = servletURL.openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + jSessionID);
// ...

This should give you the same session back in the servlet on request.getSession().

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I add this to the applet and my session had attributes at last.. but i had another error so i run my report in jasper 4.0.2 version, accordingly i change the pom file in my project and it works fine :) Thank you – falconiki Feb 16 '12 at 11:04
0

If there is a request than there must be a session. I think .getAttribute("printMB") is null. You must check before cast it to PrintingBean.

besc
  • 481
  • 3
  • 8