[英]how can i store more than one session data in httpsession?is there any other way to store
servlet code
for (int i=0;i<StudentValues.studentmap.size();i++){
System.out.println("size"+i);
HttpSession session= request.getSession();
session.setAttribute("id", std.getStudentId());
session.setAttribute("fname", std.getFirstName());
session.setAttribute("mname", std.getMiddleName());
session.setAttribute("lname", std.getLastName());
session.setAttribute("faname", std.getFatherName());
session.setAttribute("maname", std.getMotherName());
session.setAttribute("address", std.getAddressDetails());
session.setAttribute("total", std.getTotalMarks());
session.setAttribute("grade", std.getGrade());
}
在這里我創建了一個會話來保存data。但是只有最后一個數據被傳遞給jsp怎么辦
and my jsp code
<%
int size=StudentValues.studentmap.size();
%>
<%
for(int i=0;i <size;i++)
{
%>
<tr>
<td><%out.println(session.getAttribute("id"));%></td>
<td><%out.println(session.getAttribute("fname"));%></td>
<td><%out.println(session.getAttribute("mname"));%></td>
<td><%out.println(session.getAttribute("lname"));%></td>
<td><%out.println(session.getAttribute("faname"));%></td>
<td><%out.println(session.getAttribute("maname"));%></td>
<td><%out.println(session.getAttribute("address"));%></td>
<td><%out.println(session.getAttribute("total"));%></td>
<td><%out.println(session.getAttribute("grade"));%></td>
</tr>
<%
}
%>
</table>
我已經使用動態表的概念來保存那些數據,但是只有我在表中得到的最后一個數據。 我如何獲得我多次輸入的所有數據
您應該使用JSTL進行此操作。 在JSP中使用scriplets是一個壞習慣 。
在servelt中,只需將整個學生地圖設置為一個屬性:
session.setAttribute("students", studentMap);
在jsp中:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table>
<c:forEach var="student" items="${students}">
<tr>
<td>${student.mname}</td>
<td>${student.address}</td>
<td>${student.grade}</td>
</tr>
</c:forEach>
</table>
您只獲得最后一個值,因為這些名稱“ id” , “ fname” , “ mname” , “ lname” , “ faname” , “ maname” , “ address” , “ total” , “ grade”必須唯一,因此當您迭代覆蓋了先前的值,因此您只獲得列表中的最后一個值
您實際在做什么是x=5
然后x=7
然后x=9
所以您只會得到最后一個值
您可以將數組存儲在會話中,並使用JSTL標簽在jsp中進行迭代
在servlet中:
session.setAttribute("myList", studentList);
在jsp中:
<c:forEach items="${myList}" var="element">
<tr>
<td>${element.id}</td>
<td>${element.fname}</td>
<td>${element.mname}</td>
<td>${element.lname}</td>
</tr>
</c:forEach>
會話的屬性基本上是一個映射。
您可能想要做的就是只具有一個“學生”屬性,並在其中存儲學生列表。 然后遍歷該事物以創建您的表。
要將會話值存儲到會話對象中,
session.setAttribute("ref-name", Session value)
;
而不是使用
Scriptlet Tag
(<%
//Java code
%>)
像這樣使用JSTL標簽庫,
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
您可以在jsp頁面的任何位置訪問會話值。
<!-- Session Values-->
<c:set var="userId" value="${sessionScope['fname']}"></c:set>
第二種方式
session.setAttribute("students", studentList);
在JSP中使用Expression Langauage(EL - $)
<table>
<c:forEach var="student" items="${studentList}">
<tr>
<td>${student.fname}</td>
<td>${student.userid}</td>
</tr>
</c:forEach>
</table>
您可以將std對象從servlet添加到會話中,並從jsp頁面獲取它。
添加到Servlet中的會話
session.setAttribute("std",std);
從jsp會話中獲取
Student std=(Student)session.getAttribute("std");
out.println(std.getFirstName());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.