request.getSession() 为空

发布于 01-02 16:25 字数 1300 浏览 3 评论 0原文

如果有人帮助我解决以下问题,我将不胜感激。 我有一个 jasper 报告,我填写了一个 PrintingBean ,一切都很好。当我点击打印预览按钮(打开小程序)时,我的应用程序抛出一个空指针异常:

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

看起来它创建了新会话(但我在图形用户界面上看不到,这一切都很好)。我的manageBean 是一个SessionScoped。这是我的整个方法:

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城半夏2025-01-09 16:25:22

会话由名为 JSESSIONID 的 cookie 维护。通常,此 cookie 由服务器在会话开始时设置,并且在整个会话期间的每个后续单个 HTTP 请求中,此 cookie 从客户端返回到服务器。客户端(网络浏览器)透明地完成这一切。另请参阅 servlet 是如何工作的?实例化、会话、共享变量和多线程

在小程序中,您需要模拟与网络浏览器所做的相同的操作。当 applet 连接到 servlet 并需要访问与为 applet 提供服务的页面相同的会话时,您应该确保将完全相同的会话 cookie 附加到 applet 发送的 HTTP 请求中。

最简单的方法是将会话 ID 作为参数传递给小程序:

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

(注意:我假设您使用 Facelets 作为视图技术,如果您使用 JSP,那么您应该使用 ${ pageContext.session.id} 代替)

这样您就可以在小程序中相应地设置所需的会话 cookie:

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

这应该在 request.getSession() 上的 servlet 中为您提供相同的会话

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().

隱形的亼2025-01-09 16:25:22

如果有请求,则必须有会话。我认为 .getAttribute("printMB") 为空。您必须在将其转换为 PrintingBean 之前进行检查。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文