用户名打印空; javabean、jsp 会话

发布于 2025-01-04 23:54:31 字数 1615 浏览 0 评论 0原文

用户名打印 NULL,这里与 servlet 和 jsp 页面相关的问题是什么。请注意我在这里也使用javabean。我在 bean 数据库中使用相同的会话属性两次。

小服务程序一 小

    if(userPassword!=null && userPassword.equals(password)) {

   HttpSession session = request.getSession();
   BookingBean db = (BookingBean)session.getAttribute("formData"); //use existing session
   if(db == null) {
   db = new BookingBean(); }

   db.setUsername(username);
   session.setAttribute("formData", db);
  getServletContext().getRequestDispatcher("/BookingForm.jsp").forward(request, response);
    } else ......

服务程序二

     HttpSession session = request.getSession();
    if(!session.isNew()){

        db = new BookingBean();
        db.setRandom();
        db.setAdult(adults);
        db.setChildren(children);
        db.setOap(oap);
        db.setEmail(email);
        db.setDate(date);
        db.setLocation(loc);
        db.setPromo(promo);
        db.setTime(time);
        db.setfirstName(firstName);
        db.setsurName(surname);
        db.setTotal();
        session.setAttribute("formData", db);


    } 



 JSP PAGE


    <jsp:useBean id="formData" class="bean.BookingBean" scope ="session">

    </jsp:useBean>

    <%BookingBean username = (BookingBean)session.getAttribute("formData");
        if(username==null)  {
                        // if session is expired, forward it to login page

     %>
     <jsp:forward page="Login.jsp" />
    <%}%>

    <p>Confirmation of Booking for the following user: <%=formData.getUsername()%><p><br><br>

Username is printing NULL, what is the problem here in realtion to the servlet and jsp page. please note i am using javabean here too. i am using same session attribute twice with the bean db.

SERVLET ONE

    if(userPassword!=null && userPassword.equals(password)) {

   HttpSession session = request.getSession();
   BookingBean db = (BookingBean)session.getAttribute("formData"); //use existing session
   if(db == null) {
   db = new BookingBean(); }

   db.setUsername(username);
   session.setAttribute("formData", db);
  getServletContext().getRequestDispatcher("/BookingForm.jsp").forward(request, response);
    } else ......

SERVLET TWO

     HttpSession session = request.getSession();
    if(!session.isNew()){

        db = new BookingBean();
        db.setRandom();
        db.setAdult(adults);
        db.setChildren(children);
        db.setOap(oap);
        db.setEmail(email);
        db.setDate(date);
        db.setLocation(loc);
        db.setPromo(promo);
        db.setTime(time);
        db.setfirstName(firstName);
        db.setsurName(surname);
        db.setTotal();
        session.setAttribute("formData", db);


    } 



 JSP PAGE


    <jsp:useBean id="formData" class="bean.BookingBean" scope ="session">

    </jsp:useBean>

    <%BookingBean username = (BookingBean)session.getAttribute("formData");
        if(username==null)  {
                        // if session is expired, forward it to login page

     %>
     <jsp:forward page="Login.jsp" />
    <%}%>

    <p>Confirmation of Booking for the following user: <%=formData.getUsername()%><p><br><br>

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

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

发布评论

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

评论(2

离线来电— 2025-01-11 23:54:31

看起来您正在为数据使用两个不同的对象。确保您只使用一个对象。每个 servlet 都应该检查该属性是否已使用 BookingBean 实例设置并使用该实例。

确保您正在同步会话对象:

synchronized (request.getSession().getId().intern()) { 
//dosomething
}

It looks like you are using two different object for your data. Make sure, that you are only using one object. Every servlet should check, if the attribute is already set with a BookingBean instance and use this instance.

Make sure, that you are synchronizing the session object:

synchronized (request.getSession().getId().intern()) { 
//dosomething
}
℡Ms空城旧梦 2025-01-11 23:54:31

看起来您的两个 servlet 在调用时都创建了一个新的 BookingBean:

db = new BookingBean();

因此,当您将其存储到键 "formData" 下的会话时,另一个 servlet 存储的 BookingBean 会被覆盖。相反,您需要首先检查会话是否已包含该 bean,只有当会话中没有存储该 bean 时才创建一个新的。

这是检查会话是否已经拥有此对象的简单方法:

BookingBean db = (BookingBean)session.getAttribute("formData");

//HttpSession will return null, if there is no object with the given key
if(db == null)
{
    db = new BookingBean();
}

//Continue as before...

Looks like both your servlets create a new BookingBean when invoked:

db = new BookingBean();

So, when you store it to the session under key "formData", the BookingBean stored by the other servlet gets overwritten. Instead, you need to first check if the session already contains the bean, and create a new one only if there's not one stored in the session.

Here's a simple way to check if the session already has this object:

BookingBean db = (BookingBean)session.getAttribute("formData");

//HttpSession will return null, if there is no object with the given key
if(db == null)
{
    db = new BookingBean();
}

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