简体   繁体   中英

login-logout session not working properly

On using the incorrect password it displays the message failed but on using the correct message it should display a welcome message with a logout button instead it displays a blank page. Can someone please check what the problem is?

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Login Page</title>
</head>
<body>
    <form action="dashboard" method="POST">
        <input type="text" name="emailLogin" placeholder="enter your email   address" size="50" />
        <br>
        <br>            
        <input type="password" name="passLogin" placeholder="enter your   password" size="50" />
        <br>
        <br>
        <input type="submit" value="login" name="login" />
    </form>
 </body>
</html>

dashboard.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<%

session = request.getSession(false);
if(session.getAttribute("userEmail")!=null)
{
          Connection con=null;
          PreparedStatement ps;
          ResultSet rs;
          String query;

          try
          {
              Class.forName("com.mysql.jdbc.Driver");
          }catch(Exception e)
          {
              System.out.println(e);
          }

          con =   DriverManager.getConnection("jdbc:mysql://localhost:3306/userpass", "root",   "root");
            query="select * from userinfo where email=?";
            ps = con.prepareStatement(query);
            ps.setString(1, session.getAttribute("email").toString());                
            rs = ps.executeQuery();
            if(rs.next())
            {
                 out.println("welcome, "+rs.getString("name")+"!!");
                 out.println("<br>session created for you....");

                 //creating logout button

                 out.println("<form action=\"logout.jsp\" method=\"post\">");
                 out.println("<input type=\"submit\" name=\"logout\"value=\"Logout\">");
                 out.println("</form>");
            }


 }


 if("POST".equalsIgnoreCase(request.getMethod()));
 {
    if(request.getParameter("login")!=null)
    {
        if(request.getParameter("login").equals("login"))
        {
          String email = request.getParameter("emailLogin");
          String password = request.getParameter("passLogin");

          Connection con=null;
          PreparedStatement ps;
          ResultSet rs;
          String query;

          try
          {
              Class.forName("com.mysql.jdbc.Driver");
          }catch(Exception e){System.out.println(e);}

          try
          {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
            query="select * from userinfo where email=? and password=?";
            ps = con.prepareStatement(query);
            ps.setString(1, email);
            ps.setString(2, password);
            rs = ps.executeQuery();

            if(rs.next())
            {
                //login sucessfull!
                //creating session..
                session = request.getSession();
                session.setAttribute("email", email);
                response.sendRedirect("dashboard.jsp");
            }else 
            {
                out.println("failed");
            }

          }catch(SQLException e)
          {
              System.out.println(e);
          }

        }


    }
 }

%>


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Dashboard</title>
</head>
<body>

</body>
</html>

logout.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    session.invalidate();
    session = request.getSession();
    response.sendRedirect("index.jsp");


 %>

这是一个简单的语法错误,属性userEmail和email之间不匹配

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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