how to store request object in session scope in jsp

前端 未结 3 1979
南方客
南方客 2021-01-28 03:00

I was just doing random trick & testing in my jsp pages. I wanted to store request scope object in session scope object using At

3条回答
  •  感动是毒
    2021-01-28 03:40

    You can do this with following code:

    HttpSession session = request.getSession();
    session.setAttribute("req1", request);
    

    And when you retrieve the request value you have to do this as:

    <%= ((HttpServletRequest) rrrr.getAttribute("req1")).getAttribute("request1") %>
    

    After all this: If you want to store an Object in session better way is to store it directly into session, instead of storing a request as attribute in session. See below code:

    To store attribute:

    HttpSession session = request.getSession();
    session.setAttribute("obj1", Object); //  Object is any object that you want to store
    

    and retrieve it as:

    HttpSession session = request.getSession(false);
    Object o = session.getAttribute("obj1");
    

提交回复
热议问题