如何在Servlet中检索Cookie(显示所有内容)

时间:2018-10-15 17:06:28

标签: java jsp session servlets

String userNmae = req.getParameter("userName");
Cookie ck = new Cookie("hello", vall);
res.addCookie(ck);

//让我们假设,我在cookie中存储了5个不同的用户名或整数。

Cookie [] cookies = req.getCookies();
            String name;
        for(Cookie cookie : cookies) {
                 name = cookie.getValue();
                req.setAttribute("vav", name);
                req.getRequestDispatcher("index.jsp").forward(req, res);
              }

//现在,我想检索所有值并显示在jsp页面上。我该怎么办..

${vav}

jsp文件.. 谢谢大家。

2 个答案:

答案 0 :(得分:0)

如果您正在使用spring,我想您有一个要在其上显示信息的视图的控制器类。只需将存储的信息添加到给定的模型中,然后您就可以使用表达式语言进行检索了。

类似这样的东西:

@RequestMapping(value = "/login")
public String login(ModelMap model, HttpServletRequest request){
   // retrieve all data from cookies and save it somewhere in a List/Map
   model.addAttribute(cookieData);
   return "login";
}

然后在JSP中使用JSTL和EL在地图上循环并显示您需要的内容:

<c:forEach items="${cookieData}" var="cookie">
  <p>${cookieData.userName}</p>
</c:forEach>

希望这能给您一些见识。

编辑:在尝试foreach循环之前,请确保在JSP页面中导入JSTL。

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

我也同意,敏感数据绝不应该存储在Cookie中,因为它们易于访问。

答案 1 :(得分:0)

用于演示会话用法的简单示例:

@Controller
public class UserController {

    private static final String USER_HISTORY = UserController.class.getName() + "_USER_HISTORY";

    @GetMapping("/")
    public String home() {
        return "index";
    }

    @ModelAttribute
    public User getUserModelAttribute() {
        return new User();
    }

    @GetMapping("/history")
    public String getHistoryView(HttpSession httpSession, Map<String, Object> model) {
        model.put("userHistory", getUserHistory(httpSession));
        return "index";
    }

    private List<User> getUserHistory(HttpSession httpSession) {
        // check if session exists
        Object history = httpSession.getAttribute(USER_HISTORY);
        if (history != null && history instanceof List) {
            return (List<User>) history;
        }
        return new ArrayList<>();
    }

    @PostMapping("/user")
    public String saveUser(HttpSession httpSession, @ModelAttribute User user) {
        List<User> history = getUserHistory(httpSession);
        history.add(user);
        httpSession.setAttribute(USER_HISTORY, history);
        return "redirect:/";
    }
}

和jsp文件:

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>
<head></head>
<body>
    <form:form method="POST" action="/user" modelAttribute="user">
         <table>
            <tr>
                <td><form:label path="firstName">First Name</form:label></td>
                <td><form:input path="firstName"/></td>
            </tr>
            <tr>
                <td><form:label path="lastName">Last Name</form:label></td>
                <td><form:input path="lastName"/></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    </form:form>

    <a href="/history">See Your History</a>

    <c:if test="${not empty userHistory}">
        <c:forEach var="user" items="${userHistory}">
            ${user.firstName} ${user.lastName}
        </c:forEach>
    </c:if>
</body>
</html>