Servlets-Session属性变为NULL [英] Servlets-Session attribute becoming NULL

查看:63
本文介绍了Servlets-Session属性变为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是servlet和jsp的新手.在我的程序中,流程如下:

loginpage.html-> controller(servlet-在这里我创建了这样的会话)

HttpSession session = request.getSession(true);
session.setAttribute("uid", uname);
System.out.println("session:"+session.getAttribute("uid"));// shows the value of uid

-> create_user.html-> conroller(现在将uid值显示为null)-> view_customers.jsp(此处需要uid值,但其 null ).

如何避免会话属性变为空?预先感谢!

解决方案

如何避免会话属性变为空?

您可以使用HttpSessionAttributeListener监视session scope中的属性状态(添加/删除/替换).

示例代码:

public class MySessionAttributeListener implements HttpSessionAttributeListener {

    public MySessionAttributeListener() {
    }

    public void attributeAdded(HttpSessionBindingEvent sessionBindingEvent) {

        // Get the session
        HttpSession session = sessionBindingEvent.getSession();

        // Log some information
        System.out.println("[SessionAttr] " + new java.util.Date()
                + " Attribute added, session " + session + ": " + sessionBindingEvent.getName()
                + "=" + sessionBindingEvent.getValue());
    }

    public void attributeRemoved(HttpSessionBindingEvent sessionBindingEvent) {

        // Get the session
        HttpSession session = sessionBindingEvent.getSession();

        // Log some information
        System.out.println("[SessionAttr] " + new java.util.Date()
                + " Attribute removed, session " + session + ": "
                + sessionBindingEvent.getName());
    }

    public void attributeReplaced(HttpSessionBindingEvent sessionBindingEvent) {

        // Get the session
        HttpSession session = sessionBindingEvent.getSession();

        // Log some information
        System.out.println("[SessionAttr] " + new java.util.Date()
                + " Attribute replaced, session " + session + ": "
                + sessionBindingEvent.getName() + "=" + sessionBindingEvent.getValue());
    }
}

web.xml :(在web.xml中添加以下行)

<listener>
    <listener-class>com.x.y.z.MySessionAttributeListener</listener-class>
</listener>

I am new to servlets and jsp. In my program the flow is as below:

loginpage.html -> controller(servlet - here I created session like this)

HttpSession session = request.getSession(true);
session.setAttribute("uid", uname);
System.out.println("session:"+session.getAttribute("uid"));// shows the value of uid

->create_user.html-> conroller( now it shows uid value as null)->view_customers.jsp (need uid value here but its null).

How to avoid session attribute from becoming null? Thanks in advance!

解决方案

How to avoid session attribute from becoming null?

You can monitor the attribute state (added/removed/replaced) in session scope using HttpSessionAttributeListener.

Sample code:

public class MySessionAttributeListener implements HttpSessionAttributeListener {

    public MySessionAttributeListener() {
    }

    public void attributeAdded(HttpSessionBindingEvent sessionBindingEvent) {

        // Get the session
        HttpSession session = sessionBindingEvent.getSession();

        // Log some information
        System.out.println("[SessionAttr] " + new java.util.Date()
                + " Attribute added, session " + session + ": " + sessionBindingEvent.getName()
                + "=" + sessionBindingEvent.getValue());
    }

    public void attributeRemoved(HttpSessionBindingEvent sessionBindingEvent) {

        // Get the session
        HttpSession session = sessionBindingEvent.getSession();

        // Log some information
        System.out.println("[SessionAttr] " + new java.util.Date()
                + " Attribute removed, session " + session + ": "
                + sessionBindingEvent.getName());
    }

    public void attributeReplaced(HttpSessionBindingEvent sessionBindingEvent) {

        // Get the session
        HttpSession session = sessionBindingEvent.getSession();

        // Log some information
        System.out.println("[SessionAttr] " + new java.util.Date()
                + " Attribute replaced, session " + session + ": "
                + sessionBindingEvent.getName() + "=" + sessionBindingEvent.getValue());
    }
}

web.xml: (add below line in web.xml)

<listener>
    <listener-class>com.x.y.z.MySessionAttributeListener</listener-class>
</listener>

这篇关于Servlets-Session属性变为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆