Why my login session is not working? it should run out.println("Welcome "+ session.getAttribute("username") +" <a href=\"logout.jsp\" >Logout</a>"); when i logged in
Login.jsp
<% if(session.getAttribute("username")!=null) { out.println("Welcome "+ session.getAttribute("username") +" <a href=\"logout.jsp\" >Logout</a>"); } else { %> <a href="#login-box" class="login-window">Login</a> <div id="login-box" class="login-popup"> <a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a> <form method="post" class="signin" action="doLogin.jsp"> <fieldset class="textbox"> <label class="username"> <span>Username or email</span> <input id="username" name="username" value="" type="text" autocomplete="on" placeholder="Username"> </label> <label class="password"> <span>Password</span> <input id="password" name="password" value="" type="password" placeholder="Password"> </label> <button class="submit button" type="submit">Sign in</button> <!-- <input class="button" name="login" type="submit" value="Sign In"> --> <p> <a class="forgot" href="#">Forgot your password?</a> </p> </fieldset> </form>
doLogin.jsp
<%@ page import="java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %> <% String username = request.getParameter("username"); String password = request.getParameter("password"); Connection conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost/database", "root", ""); Statement st = conn.createStatement(); //System.out.println("User name [" + username + "] Password [" + password +"]"); ResultSet rs = st.executeQuery("select nama from user where nama = '" + username + "' AND password = '" + password + "'"); if (rs.next()) { response.sendRedirect("login.jsp"); } else { out.println("Login Failed,Please try Again"); %> <% //response.sendRedirect("login.jsp"); conn.close(); } } catch (Exception e) { e.printStackTrace(); } %>