Login / Logout Session | why it isn't working?
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
Code :
<%
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
Code :
<%@ 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();
}
%>
Re: Login / Logout Session | why it isn't working?
From your code I can see that there is no session attribute named "username" was placed in the session scope. So the the session.getAttribute("username") will give you null that's why it doesn't work.
Re: Login / Logout Session | why it isn't working?
i just try adding session in my doLogin.jsp
it will be like this
Login.jsp
Code :
<%
if(session.getAttribute("sesusername")!=null)
{
out.println("Welcome "+ session.getAttribute("sesusername") +" <a href=\"logout.jsp\" >Logout</a>");
}
else {
%>
doLogin.jsp
Code :
<%@ 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/jspproblog", "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())
{
session.setAttribute("sesusername", rs.getString("username"));
session.setAttribute("sespassword", rs.getString("password"));
response.sendRedirect("index.jsp");
}
else {
out.println("Login Failed,Please try Again");
%>
<%
//response.sendRedirect("login.jsp");
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
%>
it give me blank page and won't redirect right now :( it just stuck in doLogin.jsp
can u help me to show me how good way to use session?
Re: Login / Logout Session | why it isn't working?
There must be an exception thrown in you program. In your query you select a column named "nama", but you tried to read "username" and "password" from the result set. This throws an SQLException because the column name is not found.
Re: Login / Logout Session | why it isn't working?
it's working man! thanks!
wait a minute i found something wrong, after i try logged in, it still stuck in doLogin.jsp with blank page, that won't directing to index.jsp
this line
Code :
if (rs.next())
{
session.setAttribute("sesusername", rs.getString("username"));
session.setAttribute("sespassword", rs.getString("password"));
response.sendRedirect("index.jsp");
}
else {
out.println("Login Failed,Please try Again");
it should redirect user after logged in into index.jsp, but it stuck
then i try push back button in my browser, i already logged in
what happend now with that send response.sendRedirect("index.jsp");
i already clear the cache, is still the same
Re: Login / Logout Session | why it isn't working?
bump
dunno why my response.sendRedirect("index.jsp");
won't working :(