用户名打印为NULL,这对servlet和jsp页面的实际问题是什么。请注意我也在这里使用javabean。我使用bean db两次使用相同的session属性。
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>
答案 0 :(得分:0)
看起来你的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...
答案 1 :(得分:0)
您似乎正在为数据使用两个不同的对象。确保您只使用一个对象。如果已使用BookingBean实例设置属性并使用此实例,则每个servlet都应检查。
确保您正在同步会话对象:
synchronized (request.getSession().getId().intern()) {
//dosomething
}