【问题标题】:How to logout the session in java [duplicate]如何在java中注销会话[重复]
【发布时间】:2016-08-09 17:53:26
【问题描述】:
public class LogoutController1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        HttpSession session = request.getSession(false);

        if(session!=null){

            session.invalidate();
            session=null;
        }
        request.getRequestDispatcher("Login.jsp").forward(request,response);
    }
}
  1. 我的网站也允许使用相似和不同的帐户同时进行多次登录。我尝试对每次登录进行会话验证,但它不起作用。请帮助我解决与我的网站有关的这个问题。
  2. 这个代码已经在这个网站上给出了。我用同样的代码试过,但它不起作用。什么问题请给我解决方案。

【问题讨论】:

  • 你在设置什么属性?我没有看到您正在执行身份验证的任何属性?

标签: java jsp


【解决方案1】:

您需要设置一个属性进行身份验证。

登录.java

public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public Login() {
        super();
    }
    Connection conn = null;
    String next="login.jsp";
    String uname,pwd;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        uname=request.getParameter("username");
        pwd=request.getParameter("password");
        HttpSession session = request.getSession(true);
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/intranet","root","root");
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select * from login where username='"+uname+"' and password='"+pwd+"' ");
            if(rs != null){
                Member mem = new Member();
                if(rs.next()) 
                {
                    mem.setId(rs.getLong("id"));
                    mem.setFirstName(rs.getString("username"));
                    mem.setEmail(rs.getString("email"));
                    session.setAttribute("user", mem);
                    next = "index.jsp";
                }
                else
                {
                    next = "login.jsp";
                }
            }
        }
        catch(Exception ex)
        {
                System.out.println(ex);
        }
        finally
        {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            response.sendRedirect(next);
        }
    }
}

注销.java

public class Logout extends HttpServlet {
    public Logout() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if(session.getAttribute("user") != null){
            session.removeAttribute("user");
            response.sendRedirect("login.jsp");
        }
    }

【讨论】:

  • 接下来我要如何使用。是否必须创建局部变量?
  • @Manjunath 是的,你必须为下一个创建一个局部变量。我已经编辑了答案。
【解决方案2】:

您的代码仅显示注销控制器的逻辑。确保 Logincontroller 有一些逻辑来检查是否有会话正在运行,如果有,则使当前会话无效。这可以通过在注销控制器中保存一些属性/标志并在登录控制器中检索它(验证标志/属性)并相应地继续来完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2013-03-14
    • 1970-01-01
    • 2014-12-16
    • 2014-01-19
    • 2014-08-30
    • 2020-02-28
    相关资源
    最近更新 更多