简体   繁体   English

JSP:关于具有约束登录状态的系统中的会话

[英]JSP: About Session in a system with login in constraint

When I am doing a project requires a login in system, I found that jsp will automatically create session, so I add <%@ page session="false" %> into all pages to disable their abilities to create session since I only want one servlet to be able to create session. 当我做一个项目需要在系统中登录时,我发现jsp会自动创建会话,因此我在所有页面中添加<%@ page session =“ false”%>以禁用其创建会话的能力,因为我只想要一个servlet能够创建会话。

However, when it comes to using bean, I found that, I can't use bean with session scope because of <%@ page session="false" %>, I would like to ask what is the possible solution to solve this deadlock. 但是,在使用bean时,我发现由于<%@ page session =“ false”%>而无法在会话范围内使用bean,我想问一下解决此死锁的可能解决方案是什么。

Many thanks 非常感谢

If you are setting session attributes from servlet(after login) then when you move to another JSP from it, the session will retain and you do not need to write 如果要从servlet设置会话属性(登录后),那么当从servlet转移到另一个JSP时,该会话将保留,并且您无需编写

<%@ page session="false" %>

on that JSP. 在那个JSP上。 All the attributes that you set will be available for you in the session. 您设置的所有属性在会话中将对您可用。

Here's a test code : index page shows 'name' attribute set in servlet - MaintainSession also when you press 'next JSP' button, it takes you to another new JSP which again shows the 'name' attribute. 这是一个测试代码:索引页面显示了servlet中设置的“名称”属性-当您按下“下一个JSP”按钮时,也会通过MaintenanceSession进入另一个新的JSP,它再次显示“名称”属性。

None of the JSP needs the <%@ page session="false" %>. JSP都不需要<%@页面session =“ false”%>。

index.jsp index.jsp

    <body>
        <form action="MaintainSession" method="post">
            <input type="submit" value="Set Session Attribs"/>
        </form>
        <h1>Name : ${sessionScope.name}</h1>
        <h1>Name : <%=session.getAttribute("name")%></h1>
        <form action="Next.jsp" method="post">
            <input type="submit" value="Next JSP"/>
        </form>
    </body> 

Next.jsp Next.jsp

    <body>
        <h1>Hello World!</h1>
        <h1>Name : ${sessionScope.name}</h1>
        <h1>Name : <%=session.getAttribute("name")%></h1>
    </body>

MaintainSession.java MaintenanceSession.java

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       HttpSession session = request.getSession(true);
        session.setAttribute("name", "MyName");
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

The flow : 流程:

index.jsp ----Press Set Session Attribs---> MaintainSession servlet ----> index.jsp ---- press Next JSP ----> Next.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM